Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-14-2010, 01:57 AM   #1
stupidkuku
Registered User
 
Join Date: Feb 2010
Posts: 7
Default [AS3] game movement help

Hi, I'm making a simple game for my sub module, so I don't really know a lot of indepth stuff.

Basically I'm making a game whereby the player moves the cow by using the left and right arrow keys, and the spacebar to jump.

I have everything working fine, except for the cow to jump.

These are my codes.

Code:
//add keyboard event for stage for cow
stage.addEventListener(KeyboardEvent.KEY_DOWN, track_keys);
cow.addEventListener(Event.ENTER_FRAME, move_cow);

//function to track_keys
		function track_keys(e){
			// check if cow is jumping up
			if(e.keyCode == 32){ //space is 32
				trace("Cow is jumping");
				dy -= 150; 
			}
					
			if(e.keyCode == 37){ //left is 37
				trace("Cow is moving left");
				dx -= 10;
			}
			
			if(e.keyCode == 39){ //right is 39
				trace("Cow is moving right");
				dx += 10;
			}			
		}//end of function_track_keys
		
		
		
		
		
		//function to move cow
		function move_cow(e){	
			//limit movement
			if(dy < 0){				//don't move up
				dy = 0;
			}
			
			if(dx < 0){				//don't move left
				dx = 0;
			}
			
			if(dy > 253){			//don't move down
				dy = 253;
			}
			
			if(dx > 499){			//don't move right
				dx = 499;
			}
			
			
			e.currentTarget.x += (dx-e.currentTarget.x)/10;
			e.currentTarget.y += (dy-e.currentTarget.y)/10;
		
		}

Left and right works fine, but when I press the spacebar, it jumps and stays in that position? How do I get it to come back down?

And I've been searching around for solutions, and I've learned about creating an object in Flash and then right clicking it to convert it to a movieclip. I've successfully made a box jump, but my cow is a swf file and contains 4 images, and I've not yet found a tutorial that solves my problem.

Is there anyone who can help me? I've read about gravity and hitTest and I think they can solve my problems, but I'm not sure how to use them. I'm very new to this programming language and may not know most of the terms used, so please be patient with me. Thanks.

PS: All my codes are in Flex Builder 3. Is it possible to make the swf file jump without having to go into movieclip?

Last edited by stupidkuku; 02-14-2010 at 02:39 AM.
stupidkuku is offline   Reply With Quote
Old 02-14-2010, 02:06 AM   #2
nanook
Member
 
Join Date: Feb 2010
Posts: 72
Default

I don't see any code that would cause the cow to come back down. I only see code to keep it from coming down.

BTW, it helps the readability of your post if you wrap your code in the "" tags when you post it (use the "#" in the toolbar above the text area window).
nanook is offline   Reply With Quote
Old 02-14-2010, 02:29 AM   #3
stupidkuku
Registered User
 
Join Date: Feb 2010
Posts: 7
Default

Quote:
Originally Posted by nanook View Post
I don't see any code that would cause the cow to come back down. I only see code to keep it from coming down.

BTW, it helps the readability of your post if you wrap your code in the "" tags when you post it (use the "#" in the toolbar above the text area window).
Thanks. I've put them inside the code tags.

My friend told me that I should create a code for the cow to come back down, but I don't know how to. =(
stupidkuku is offline   Reply With Quote
Old 02-14-2010, 04:15 AM   #4
sparX
!Senior Member
 
Join Date: Jan 2010
Posts: 1,675
Default

how about,

ActionScript Code:
stage.addEventListener(KeyboardEvent.KEY_UP, dropCow); function dropCow (e:KeyboardEvent):void {          if (e.keyCode == 32)                 dy +=150; }
sparX is offline   Reply With Quote
Old 02-14-2010, 04:37 AM   #5
stupidkuku
Registered User
 
Join Date: Feb 2010
Posts: 7
Default

Quote:
Originally Posted by sparX View Post
how about,

ActionScript Code:
stage.addEventListener(KeyboardEvent.KEY_UP, dropCow); function dropCow (e:KeyboardEvent):void {          if (e.keyCode == 32)                 dy +=150; }
I've tried that. When I pressed the spacebar, it moves up for a short distance and then quickly comes back then. This is a problem as the objects to jump over are rather huge. Also, when I hold the spacebar, the cow flys and stays up there, and another tap of the spacebar brings it back vertically down.

How can I code so that the cow jumps over a parabolic curve?
stupidkuku is offline   Reply With Quote
Old 02-14-2010, 08:31 AM   #6
TheElusiveTrout
Member
 
Join Date: Nov 2009
Posts: 54
Default

Use accelerations and velocities.

Make variables. verticalVelocity, gravity and terminalVelocity. In the cow's looping function (Event.ENTER_FRAME or w/e you have), put something like this
ActionScript Code:
verticalVelocity -= gravity; if(verticalVelocity < terminalVelocity)      verticalVelocity = terminalVelocity; cow.y -= verticalVelocity; if(cow.y > stageHeight)        cow.y = stageHeight;
Then whenever the jump button is pressed set the cow's verticalVelocity to some positive number. He'll rise fast as first, but the gravity will start to pull him back down, until finally he's falling. And the check on the cow's height at the end will stop it from falling through the floor.

Of course this is the bare bare minimum framework to create and arc jump. You'll want to expand on it.
TheElusiveTrout is offline   Reply With Quote
Old 02-14-2010, 01:07 PM   #7
stupidkuku
Registered User
 
Join Date: Feb 2010
Posts: 7
Default

Quote:
Originally Posted by TheElusiveTrout View Post
Use accelerations and velocities.

Make variables. verticalVelocity, gravity and terminalVelocity. In the cow's looping function (Event.ENTER_FRAME or w/e you have), put something like this
ActionScript Code:
verticalVelocity -= gravity; if(verticalVelocity < terminalVelocity)      verticalVelocity = terminalVelocity; cow.y -= verticalVelocity; if(cow.y > stageHeight)        cow.y = stageHeight;
Then whenever the jump button is pressed set the cow's verticalVelocity to some positive number. He'll rise fast as first, but the gravity will start to pull him back down, until finally he's falling. And the check on the cow's height at the end will stop it from falling through the floor.

Of course this is the bare bare minimum framework to create and arc jump. You'll want to expand on it.
Very grateful for your reply. I have tried them out, but I still couldn't get what I want.

Code:
//vertical velocity
		var vertical_velocity:Number = 1;
		
		//terminal velocity
		var terminal_velocity: Number = 2; //max velocity cow can reach
		
		//gravity
		var gravity:Number = 0.5; // drop speed increase per frame
Code:
//define start_game function
		function start_game(e){
			trace("Starting Game");
		
			//load the cow
			imgURL = new URLRequest("cow.swf");
			cow.load(imgURL);
			addChild(cow);
			cow.x = 5;
			cow.y = 253;
			
			//add keyboard event for stage for cow
			stage.addEventListener(KeyboardEvent.KEY_DOWN, track_keys);
			cow.addEventListener(Event.ENTER_FRAME, move_cow);
		
		}
Code:
//function to track_keys
		function track_keys(e){
			// check if cow is jumping up
			if(e.keyCode == 32){ //space is 32
				trace("Cow is jumping");
				dy -= 300; 
			}
			
			//if(e.keyCode == 40){ //down is 40
			//	trace("Cow is moving down");
			//	dy += 10;
			//}
			
			if(e.keyCode == 37){ //left is 37
				trace("Cow is moving left");
				dx -= 10;
			}
			
			if(e.keyCode == 39){ //right is 39
				trace("Cow is moving right");
				dx += 10;
			}			
		}//end of function_track_keys
Code:
//function to move cow
		function move_cow(e){	
			//limit movement
			if(dy < 0){				//don't move up
				dy = 0;
			}
			
			if(dx < 0){				//don't move left
				dx = 0;
			}
			
			if(dy > 253){			//don't move down
				dy = 253;
			}
			
			if(dx > 499){			//don't move right
				dx = 499;
			}
			
			
			e.currentTarget.x += (dx-e.currentTarget.x)/10;
			e.currentTarget.y += (dy-e.currentTarget.y)/10;
			
			vertical_velocity -= gravity;
			if(vertical_velocity < terminal_velocity){
				vertical_velocity = terminal_velocity;
				cow.y -= vertical_velocity;
			}
			
			if(cow.y > 253){
				cow.y = 253;
			}
		}
Did I go wrong somewhere? Please help me. I've been trying to solve this for many days.
stupidkuku is offline   Reply With Quote
Old 02-14-2010, 02:41 PM   #8
nanook
Member
 
Join Date: Feb 2010
Posts: 72
Default

Since you said that you don't know a lot of indepth stuff you might want to try using tweens for you motions. I prefer Tweenlite; you can find it here:
http://www.greensock.com/

Arc motion can done using the "Bezier-through" plug-in.
nanook is offline   Reply With Quote
Old 02-15-2010, 03:29 AM   #9
stupidkuku
Registered User
 
Join Date: Feb 2010
Posts: 7
Unhappy help with Jump problem

Hi, I'm trying to make a simple game whereby the player controls a cow by moving the left and right arrow keys and the spacebar to jump. Basically the cow will start from the bottom left hand corner of the screen and will walk on the moving platform coming in from the right. There will be objects on the ground that the cow will have to jump over to score points.




My problem is that it seems like I can never get the cow to jump at all, well, maybe jumping yes, but it doesn't come back down after a jump. My cow is a swf file(101x103) that I had created using flash and I've seen tutorials requiring having to convert that to movieclip in order to make things jump.

I'm grateful to those who have helped me, but I still can't get it done.

My latest help got me to these codes:

Code:
//vertical velocity
		var vertical_velocity:Number = 1;
		
		//terminal velocity
		var terminal_velocity: Number = 2; //max velocity cow can reach
		
		//gravity
		var gravity:Number = 0.5; // drop speed increase per frame
Code:
//define start_game function
		function start_game(e){
			trace("Starting Game");
		
			//load the cow
			imgURL = new URLRequest("cow.swf");
			cow.load(imgURL);
			addChild(cow);
			cow.x = 5;
			cow.y = 253;
			
			//add keyboard event for stage for cow
			stage.addEventListener(KeyboardEvent.KEY_DOWN, track_keys);
			cow.addEventListener(Event.ENTER_FRAME, move_cow);
		
		}
Code:
//function to track_keys
		function track_keys(e){
			// check if cow is jumping up
			if(e.keyCode == 32){ //space is 32
				trace("Cow is jumping");
				dy -= 300; 
			}
			
			//if(e.keyCode == 40){ //down is 40
			//	trace("Cow is moving down");
			//	dy += 10;
			//}
			
			if(e.keyCode == 37){ //left is 37
				trace("Cow is moving left");
				dx -= 10;
			}
			
			if(e.keyCode == 39){ //right is 39
				trace("Cow is moving right");
				dx += 10;
			}			
		}//end of function_track_keys

Code:
//function to move cow
		function move_cow(e){	
			//limit movement
			if(dy < 0){				//don't move up
				dy = 0;
			}
			
			if(dx < 0){				//don't move left
				dx = 0;
			}
			
			if(dy > 253){			//don't move down
				dy = 253;
			}
			
			if(dx > 499){			//don't move right
				dx = 499;
			}
			
			
			e.currentTarget.x += (dx-e.currentTarget.x)/10;
			e.currentTarget.y += (dy-e.currentTarget.y)/10;
			
			vertical_velocity -= gravity;
			if(vertical_velocity < terminal_velocity){
				vertical_velocity = terminal_velocity;
				cow.y -= vertical_velocity;
			}
			
			if(cow.y > 253){
				cow.y = 253;
			}
		}


The cow starts out floating a little above the platform and when I press spacebar, it jumps but doesn't comes back down.

I would like the cow to jump without having to use moveclip or symbol. If it's required, can someone kindly give me a step by step explanation? I'm really desperate to find a solution to this problem. =(
stupidkuku is offline   Reply With Quote
Old 02-15-2010, 05:17 AM   #10
TheElusiveTrout
Member
 
Join Date: Nov 2009
Posts: 54
Default

Change last two functions to this. You weren't actually implementing the code I gave you, and had some measurements mixed up. From experience the current values probably won't provide a good jump.

I'd start with
terminal_velocity = 15;
gravity = 1;
and when you jump set vertical_velocity = 15;

That will give you a starting place. I assume you're left right movement code works since you've haven't asked about it and I didn't want to figure out what it was doing.

ActionScript Code:
//function to track_keys         function track_keys(e){             // check if cow is jumping up             if(e.keyCode == 32){ //space is 32                 trace("Cow is jumping");                 vertical_velocity = 5;             }                         if(e.keyCode == 37){ //left is 37                 trace("Cow is moving left");                 dx -= 10;             }                         if(e.keyCode == 39){ //right is 39                 trace("Cow is moving right");                 dx += 10;             }                  }//end of function_track_keys


ActionScript Code:
//function to move cow         function move_cow(e){                //limit movement                    if(dx < 0){    //don't move left                 dx = 0;             }             if(dx > 499){         //don't move right                 dx = 499;             }                                     e.currentTarget.x += (dx-e.currentTarget.x)/10;                 vertical_velocity -= gravity;             if(vertical_velocity < -terminal_velocity){                 vertical_velocity = terminal_velocity;             }             cow.y -= vertical_velocity;             if(cow.y > 253){                 cow.y = 253;             }         }
TheElusiveTrout is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:24 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.