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