PDA

View Full Version : [AS2] Platform game help


Timbo5000
11-24-2010, 08:38 PM
i am making a game in which platforms move from left to right and you are a black ball(mc_ball). i followed a tutorial for jumping and platform barriers and tried to apply the tecnique to 5 blocks instead of 1. this is my code for movement. all the happens when i touch the block is that i slow down a bit.

any help appreciated.

here is the code on my mc_ball
(please bare in mind i am very new to flash/actionscript) :)

onClipEvent (enterFrame) {
speed = 5;
if (Key.isDown(Key.RIGHT)) {
this._x = this._x+speed;
}
if (Key.isDown(Key.LEFT)) {
this._x = this._x-speed;
}
if (Key.isDown(Key.DOWN)) {
this._y = this._y+speed;
}
}
onClipEvent(load) {
falling = false;
jumping = false;
gravity = 12;
jump = 0
}
onClipEvent(enterFrame) {

// enemy 1
// if not touching enemy 1 or jumping, you are falling
if(!_root.enemy1.hitTest(this._x,this._y,true) && !jumping) {
_y += 1.5
}
// if you are falling and touch enemy1, you stop
if(_root.enemy1.hitTest(this._x,this._y,true) && falling) {
falling = false;
jumping = false;
jump = 12;
}
if(Key.isDown(Key.UP)) {
// when up is pressed, jumping is true
jumping = true;
}
// if jump < 0 then you have to be falling
if (jump<0) {
falling = true;
}
if(jumping) {
this._y -= jump;
jump -= .9;
}
//enemy 2
// if not touching enemy 2 or jumping, you are falling
if(!_root.enemy2.hitTest(this._x,this._y,true) && !jumping) {
_y += 1.5
}
// if you are falling and touch enemy2, you stop
if(_root.enemy2.hitTest(this._x,this._y,true) && falling) {
falling = false;
jumping = false;
jump = 12;
}
//enemy 3
// if not touching enemy 3 or jumping, you are falling
if(!_root.enemy3.hitTest(this._x,this._y,true) && !jumping) {
_y += 1.5
}
// if you are falling and touch enemy3, you stop
if(_root.enemy3.hitTest(this._x,this._y,true) && falling) {
falling = false;
jumping = false;
jump = 12;
}
//enemy 4
// if not touching enemy 4 or jumping, you are falling
if(!_root.enemy4.hitTest(this._x,this._y,true) && !jumping) {
_y += 1.5
}
// if you are falling and touch enemy4, you stop
if(_root.enemy4.hitTest(this._x,this._y,true) && falling) {
falling = false;
jumping = false;
jump = 12;
}
//enemy 5
// if not touching enemy 5 or jumping, you are falling
if(!_root.enemy5.hitTest(this._x,this._y,true) && !jumping) {
_y += 1.5
}
// if you are falling and touch enemy5, you stop
if(_root.enemy5.hitTest(this._x,this._y,true) && falling) {
falling = false;
jumping = false;
jump = 12;
}
}

hey
11-28-2010, 03:00 PM
Looks like you had a good start, but I'd do it a bit differently. Here's a quick modification of your code:

onClipEvent (load) {
falling = false;
jumping = false;
grav = 0;
jump = 0;
speed = 5;

//Array which holds all of the enemies
Enemy = [_root.enemy1, _root.enemy2, _root.enemy3, _root.enemy4, _root.enemy5];
}
onClipEvent (enterFrame) {

if (Key.isDown(Key.RIGHT)) {
this._x += speed;
}
if (Key.isDown(Key.LEFT)) {
this._x -= speed;
}

//Gravity increases untill you reach terminal velocity
grav += 1;
if(grav > 12){
grav = 12;
}
falling = true;

//This loops through all of the enemies
for (i=0; i< Enemy.length; i++) {
if (Enemy[i].hitTest(this._x, this._y, true)) {
falling = false;
jumping = false;
jump = 12;
grav = 0;

}
}

if (Key.isDown(Key.DOWN) && (falling == true)) {
//If down is pressed an you are falling, you move down faster
this._y += speed;
}
if (Key.isDown(Key.UP) && (falling == false)) {
// when up is pressed and you're not falling, jumping is true
jumping = true;
}

// if jump < 0 then you aren't jumping
if (jump<0) {
jumping = false;
}

//If the player is jumping
if (jumping) {
this._y -= jump;
jump -= .9;
grav = 0;
}

//Effect of gravity
this._y += grav;


}

It's not really necessary, but you should probably avoid using two enterFrame events. It's been awhile since I've used as2, but I believe you'll have a better control over your program if you only have one. If you ever need to post code again, try using the actionscript tags since they make your code a bit easier to read. I used an array in my example, which you may, or may not, be familiar with, but it was just used to avoid writing out separate if statements for each enemy. It wasn't necessary, but I think it makes the code easier on the eyes. If you have any more questions, feel free to ask.

Hope that helps!

Timbo5000
11-28-2010, 03:09 PM
this is great thanks, except when i land on the enemys the ball is only like 2/3 showing, the other 1/3 being in the block, any fix?

hey
11-28-2010, 03:12 PM
That's because the player moves in steps which increase as the grav variable increases. When grav is pretty large, it's possible that the collision detection won't be recognized when you'd like it to be. This isn't too hard to fix, and it can be done in a couple of ways. I'll add one solution quickly.

Edit: Try this:

onClipEvent (load) {
falling = false;
jumping = false;
grav = 0;
jump = 0;
speed = 5;

//Array which holds all of the enemies
Enemy = [_root.enemy1, _root.enemy2, _root.enemy3, _root.enemy4, _root.enemy5];
}
onClipEvent (enterFrame) {

if (Key.isDown(Key.RIGHT)) {
this._x += speed;
}
if (Key.isDown(Key.LEFT)) {
this._x -= speed;
}

//Gravity increases untill you reach terminal velocity
grav += 1;
if(grav > 12){
grav = 12;
}
falling = true;

//This loops through all of the enemies
for (i=0; i< Enemy.length; i++) {
if (Enemy[i].hitTest(this._x, this._y, true)) {
falling = false;
jumping = false;
jump = 12;
grav = 0;
}

while(Enemy[i].hitTest(this._x, this._y-1, true) && (falling == false)) {
this._y--;
}
}

if (Key.isDown(Key.DOWN) && (falling == true)) {
//If down is pressed an you are falling, you move down faster
this._y += speed;
}
if (Key.isDown(Key.UP) && (falling == false)) {
// when up is pressed and you're not falling, jumping is true
jumping = true;
}

// if jump < 0 then you aren't jumping
if (jump<0) {
jumping = false;
}

//If the player is jumping
if (jumping) {
this._y -= jump;
jump -= .9;
grav = 0;
}

//Effect of gravity
this._y += grav;


}

hey
11-28-2010, 03:18 PM
The while statement will push the player up until it's no longer colliding with the enemy. One possible problem of doing it like this, is that you may get an undesirable effect if the player is on one enemy with another enemy above itself and you jump up into the above enemy. There's a term for this, but I can't remember it right now.

Timbo5000
11-28-2010, 04:04 PM
hmm it still does the same thing :( also, if i want to make the enemys randomly spawn and move from left to right(that bit is not hard, making them spawn is) would this code work fine?

hey
11-28-2010, 04:33 PM
The problem might be where the registration point is. For my code, the registration point would ideally be at the bottom of the player. With some modification, I'm sure it'd work with the enemies moving around and stuff.

Timbo5000
11-28-2010, 04:44 PM
The problem might be where the registration point is. For my code, the registration point would ideally be at the bottom of the player. With some modification, I'm sure it'd work with the enemies moving around and stuff.

this works great now i changed the registration point to the bottom :)))

You got any idea how to make it so i can have constant enemys spawning and moving from right to left? that really has me stumped (:

hey
11-28-2010, 05:08 PM
Probably the easiest way of handling that would be to have them constantly moving to the right and when they reach a certain point, you set their position to the left of the screen. I'm a bit busy right now, but I'll write an example later today(hopefully). When you're doing this, be mindful of where their registration points are (you'll want to make sure the entire object is off the screen and not just the registration point).

Timbo5000
11-28-2010, 05:19 PM
Probably the easiest way of handling that would be to have them constantly moving to the right and when they reach a certain point, you set their position to the left of the screen. I'm a bit busy right now, but I'll write an example later today(hopefully). When you're doing this, be mindful of where their registration points are (you'll want to make sure the entire object is off the screen and not just the registration point).

that sounds good, would i be able to randomize the locations of the blocks so that when they respawn at the other side its not just in the same location. I imagine it would be pretty easy, but the y difference would have to be smaller or equal to the height of the jump i am guessing.

Timbo5000
11-28-2010, 08:10 PM
i have been trying to get it so if the ball goes off the side, or bottom they go to the game over screen(scene called "End"). For some reason it doesnt work, any tips?


onClipEvent (load) {
falling = false;
jumping = false;
grav = 0;
jump = 0;
speed = 5;
bottom = 396.9;
leftwall = -18;
rightwall = 548;

//Array which holds all of the enemies
Enemy = [_root.enemy1, _root.enemy2, _root.enemy3, _root.enemy4, _root.enemy5];
}
onClipEvent (enterFrame) {

if (this._y == bottom) {
gotoAndStop("End",1);
}
if (this._x == leftwall){
gotoAndStop("End",1);
}
if (this._x == rightwall) {
gotoAndStop("End",1);
}
if (Key.isDown(Key.RIGHT)) {
this._x += speed;
}
if (Key.isDown(Key.LEFT)) {
this._x -= speed;
}

//Gravity increases untill you reach terminal velocity
grav += 1;
if(grav > 12){
grav = 12;
}
falling = true;

//This loops through all of the enemies
for (i=0; i< Enemy.length; i++) {
if (Enemy[i].hitTest(this._x, this._y, true)) {
falling = false;
jumping = false;
jump = 12;
grav = 0;
}

while(Enemy[i].hitTest(this._x, this._y-1, true) && (falling == false)) {
this._y--;
}
}

if (Key.isDown(Key.DOWN) && (falling == true)) {
//If down is pressed an you are falling, you move down faster
this._y += speed;
}
if (Key.isDown(Key.UP) && (falling == false)) {
// when up is pressed and you're not falling, jumping is true
jumping = true;
}

// if jump < 0 then you aren't jumping
if (jump<0) {
jumping = false;
}

//If the player is jumping
if (jumping) {
this._y -= jump;
jump -= .9;
grav = 0;
}

//Effect of gravity
this._y += grav;

}

hey
11-29-2010, 03:50 PM
I'm not sure if I've ever used scenes, but I'd still recommend avoiding them. I'm pretty sure they were mostly intended to be used only for animations and not for something like this. The problem is probably that your using "==". Since your movieclips move in steps, you should probably use "<=" and ">=".

Randomizing the positions shouldn't be too bad. What I'd do is look at the previous enemies position by using the array they're all in and adding a random number to it's y-value. If you do it like this, you'll have to make sure their position doesn't get too high or low.