11 Replies - 2864 Views - Last Post: 23 April 2013 - 06:56 AM

#1 gurre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-April 13

How should I continue?

Posted 20 April 2013 - 02:13 PM

Hello,

I'm making a car game and right now I'm trying to code so the car moves when I press down the keys. I have this code right now:
var speed:Number = 0;
var myRotation:Number = myCar.Rotation;
 
 
if ((myCar.Rotation < 180)&&(myCar.Rotation >= 0))
        {  
        myRotation = myCar.Rotation;  
        }
 
if ((myCar.Rotation < 0)&&(myCar.Rotation > -180))
        {  
        myRotation = -1 * myCar.Rotation;    
        }


But right now I'm stucked. I don't know what I should add next. I'm thankful for all help.

Is This A Good Question/Topic? 0
  • +

Replies To: How should I continue?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: How should I continue?

Posted 20 April 2013 - 02:21 PM

Continue how so? What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 gurre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-April 13

Re: How should I continue?

Posted 21 April 2013 - 12:46 AM

View Postmacosxnerd101, on 20 April 2013 - 02:21 PM, said:

Continue how so? What specific problems or errors are you encountering?


I mean like, what should I add after that code in order to get the car to move while holding down a key? Because that code doesn't make the car to move forward when I press the up key.
Was This Post Helpful? 0
  • +
  • -

#4 gurre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-April 13

Re: How should I continue?

Posted 22 April 2013 - 05:22 AM

Hello,

I'm making a car game and I have made so the car moves when I press the keys. But now I want to car to slow down when it drives into the walls. How do I do that? This is my code:

package AS {
	
	import flash.display.MovieClip;
	import flash.events.*;
	
	
	public class Car extends MovieClip {
		
		private var speed:Number = 0;
		private var keys:Array = [false,false,false,false];
		
		public function Car() 
		{
			super();
		}
		
		
		
		public function update(keys:Array):void
		{
			this.keys = keys;
			
			if (keys[0]) {
				speed += 1.5;
			}else if(!keys[0]&&!keys[1]){
				speed = 0;
			}
			speed = speed * 0.98;
				
		 
			if (keys[1])
			{
				speed -= 0.8;
			}
			   
			if (keys[2])
			{
				this.rotation += 8;
			}
			   
			if (keys[3])
			{
				this.rotation -= 8;
			}
			   
			this.x += Math.sin (this.rotation * Math.PI / 180) * speed;
			this.y += Math.cos (this.rotation * Math.PI / 180) * -speed;
		 
			if (Math.abs (speed) > 20)
			{
				speed = 20;
			}
			   
			if (speed < -10)
			{
				speed = -10;
			}
		}
	}
	
}

package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import AS.Car;
	import flash.events.KeyboardEvent;
	
	public class Main extends MovieClip 
	{
		
		var timer:CustomTimer;
		var car:Car;
		private var keys:Array = [false,false,false,false];
		
		public function Main() 
		{
			addEventListener(Event.ENTER_FRAME, update);
			
			trace(stage);
			addEventListener(Event.ADDED_TO_STAGE, init);
			
			
			car = new Car();
			addChild(car);
			
			timer = new CustomTimer();
			addChild(timer);
		}
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, setKeyUp);
			
			car.x = stage.width / 2;
			car.y = stage.height / 2;
		}
		
		public function update(e:Event):void
		{
			timer.update();
			car.update(keys);
		}
		
		private function setKeyDown(event:KeyboardEvent):void{
			switch(event.keyCode){
				case 38:
					keys[0] = true;
					break;
				case 40:
					keys[1] = true;
					break;
				case 39:
					keys[2] = true;
					break;
				case 37:
					keys[3] = true;
					break;
			}
		}
		
		private function setKeyUp(event:KeyboardEvent):void{
			switch(event.keyCode){
				case 38:
					keys[0] = false;
					break;
				case 40:
					keys[1] = false;
					break;
				case 39:
					keys[2] = false;
					break;
				case 37:
					keys[3] = false;
					break;
			}
		}
	}
	
}


Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8379
  • View blog
  • Posts: 31,147
  • Joined: 12-June 08

Re: How should I continue?

Posted 22 April 2013 - 07:56 AM

I would figure your walls are some sort of object, right? When the car and the wall collide just decrease the car's velocity by a bit.. It's all about the collision detection!
Was This Post Helpful? 0
  • +
  • -

#6 gurre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-April 13

Re: How should I continue?

Posted 22 April 2013 - 08:49 AM

View Postmodi123_1, on 22 April 2013 - 07:56 AM, said:

I would figure your walls are some sort of object, right? When the car and the wall collide just decrease the car's velocity by a bit.. It's all about the collision detection!


I have created the walls in Photoshop. Should I transfer them into flash and make it into a symbol? Could I do an if-statement like if wall collide with the car, slow down the velocity of the car.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8379
  • View blog
  • Posts: 31,147
  • Joined: 12-June 08

Re: How should I continue?

Posted 22 April 2013 - 08:59 AM

Well the game doesn't know how to interpret a picture, right? You would need some sort of invisible rectangle/polygon/what ever to signify a wall is supposed to be interacted with by the car. Once that is created then off you go with collision detection.
Was This Post Helpful? 0
  • +
  • -

#8 gurre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-April 13

Re: How should I continue?

Posted 22 April 2013 - 12:53 PM

View Postmodi123_1, on 22 April 2013 - 08:59 AM, said:

Well the game doesn't know how to interpret a picture, right? You would need some sort of invisible rectangle/polygon/what ever to signify a wall is supposed to be interacted with by the car. Once that is created then off you go with collision detection.


Alright, when I code the collision detection. Should I use an if-statement?
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8379
  • View blog
  • Posts: 31,147
  • Joined: 12-June 08

Re: How should I continue?

Posted 22 April 2013 - 12:55 PM

I don't see a reason why you shouldn't, do you?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: How should I continue?

Posted 22 April 2013 - 01:17 PM

Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#11 gurre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-April 13

Re: How should I continue?

Posted 23 April 2013 - 03:20 AM

View Postmodi123_1, on 22 April 2013 - 12:55 PM, said:

I don't see a reason why you shouldn't, do you?


Alright. Then I make the walls into a object and use hitTestObject:
if(carobjecthere.hitTestObject(theobjectforwallshere))
speed = -5;


Could I do like that?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8379
  • View blog
  • Posts: 31,147
  • Joined: 12-June 08

Re: How should I continue?

Posted 23 April 2013 - 06:56 AM

I don't see why not, but why don't you go off and test it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1