PDA

View Full Version : [AS3] game movement help


stupidkuku
02-14-2010, 01:57 AM
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.


//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?

nanook
02-14-2010, 02:06 AM
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).

stupidkuku
02-14-2010, 02:29 AM
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. =(

sparX
02-14-2010, 04:15 AM
how about,


stage.addEventListener(KeyboardEvent.KEY_UP, dropCow);

function dropCow (e:KeyboardEvent):void {
if (e.keyCode == 32)
dy +=150;
}

stupidkuku
02-14-2010, 04:37 AM
how about,


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?

TheElusiveTrout
02-14-2010, 08:31 AM
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

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.

stupidkuku
02-14-2010, 01:07 PM
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

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.


//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



//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);

}



//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



//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.

nanook
02-14-2010, 02:41 PM
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.

stupidkuku
02-15-2010, 03:29 AM
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.

http://i205.photobucket.com/albums/bb134/stupidkuku/cow.png


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:


//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


//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);

}


//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



//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. =(

TheElusiveTrout
02-15-2010, 05:17 AM
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.

//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


//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;
}
}

stupidkuku
02-15-2010, 05:45 AM
OMG thank you so very much!

The jump is perfect, I love it. But only one last problem: the cow jumps non stop! Why's that?

TheElusiveTrout
02-15-2010, 06:36 AM
Sorry, forgot a negative sign :rolleyes:. It should look like this.

if(vertical_velocity < -terminal_velocity)
{
vertical_velocity = -terminal_velocity
}

So what would happen before is the cow would start coming back down, and once it reached terminal velocity, suddenly its vertical_velocity is telling it to go up again.

stupidkuku
02-15-2010, 06:49 AM
OMG it works now! Thank you so much!

I can get it back down now, and it jumps when I press the spacebar. But
the jump wasn't as smooth as it was before, when it was jumping non stop.

Now, it jumps perfect, at upon reaching the peak of the jump, it drops
vertically downwards, usually on the obstacle D=

And it stops moving right even holding the right arrow key and landing after
a jump. (ToT)

Karma + 99999999999 for all your help!

TheElusiveTrout
02-15-2010, 05:22 PM
Let me give you the code I use to move my objects back and forth with the arrows. First I create 2 boolean values

var movingUp:Boolean = false;
var movingDown:Boolean = false;


Then add event listeners for both key_up and key_down

stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);

public function handleKeyDown(e:KeyboardEvent):void
{
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");
movingLeft = true;
}

if(e.keyCode == 39){ //right is 39
trace("Cow is moving right");
movingRight = true;
}
}

public function handleKeyDown(e:KeyboardEvent):void
{
if(e.keyCode == 37){ //left is 37
trace("Cow is moving left");
movingLeft = false;
}

if(e.keyCode == 39){ //right is 39
trace("Cow is moving right");
movingRight = false;
}
}

Then in your ENTER_FRAME function reference the Booleans to if you should move.


if(movingLeft)
{
this.x -= 10;
if(this.x < 0) this.x = 0;
}
if(movingRight)
{
this.x += 10;
if(this.x > *your stage width*) this.x = *your stage width*;
}

You'll have to remove all your old dx code.


As for making your jump smooth, that's all about messing with your variables.