PDA

View Full Version : Working on a platformer


pachufir
06-26-2008, 05:19 AM
hi everyone, im new to the forums... anyway i was working on a platformer game and i would like some help.

im new to actionscript in general but so far i've been able to build a level for a platformer game, the thing is its quite inefficeint and bloated. on of the main problems im facing is that if i create a "platform" object it wont let me re-use it, so then i have to create a new object for each platform. I then have to test each platform in a hit test using a series of "or" statements... but thats quite bloated and inefficient as i've said before... so could someone help me by showing me a better way to do this which might be more effective and less bloated..?
here is the code i have on my character at the moment...
onClipEvent (load) {

originx = _x;
originy = _y;

}


onClipEvent (enterFrame) {

gravity = 7;

if(_root.spikes.hitTest(this.feet) == true || _root.spikes2.hitTest(this.feet) == true || _root.spike.hitTest(this) == true || _root.spike2.hitTest(this) == true ){
_root.lives -= 1;
_x = originx;
_y = originy;

}
if(_root.lives < 0){
_root.gotoAndPlay(3);

}
if(_root.door.hitTest(this.feet) == true){
_root.gotoAndPlay(2);
}

if (Key.isDown(Key.LEFT)){
_x -= 10;
gotoAndStop(2);
feet.play();

}
if (Key.isDown(Key.RIGHT)){
_x += 10;
gotoAndStop(1);
feet.play();

}
if(Key.isDown(Key.UP)){

if(_root.ground.hitTest(this.feet) == true || _root.platform.hitTest(this.feet) == true || _root.platform2.hitTest(this.feet) == true || _root.platform3.hitTest(this.feet) == true){
jump = 5;
}
}
if(_root.ground.hitTest(this.feet) == true || _root.platform.hitTest(this.feet) == true || _root.platform2.hitTest(this.feet) == true || _root.platform3.hitTest(this.feet) == true){
gravity = 0;
}

if(jump > 0){
_y -= 20;
jump--;
}



_y += gravity;

}


if anyone wants it i can also upload the entire file, thanks :)

GMaker0507
06-26-2008, 11:14 PM
What you do is first, create an array on the _root holding all the platforms

var GamePlatforms:Array=new Array()

Then, on the first frame of the platform mc push(add) it to the array
_root.GamePlatforms.push(this)

Finally,in your character loop through all of the platforms using a for each...in statement. NOTE: You should put the jump code behind the collision detection code, it will save you time and is easier.

// Have we found something to land on
var foundGround:Boolean=false;

//Loop through all the platforms in the array
for each (var platform in _root.GamePlatforms) {
// If we find one that our feet hits
if (platform.hitTest(this.Feet)) {
// true, we have found ground, and break out of this loop to save some time
foundGround=true;
break;
}
}

/* This is just here to check if we hit '_root.Ground'. In this statement, 'true' is the dominant result. So if 'foundGround' was previously false and '_root.Ground.hitTest(this.Feet)' is true, or vice versa, it is true.It is only false if both are false*/

foundGround=foundGround||_root.Ground.hitTest(this .Feet);

if (foundGround) {
gravity=0;
if (Key.isDown(Key.UP)) {
jump = 5;
}
}

pachufir
06-28-2008, 03:33 AM
well i tried ur code, but for some reason the "each" is not working for me, the compiler is getting errors... maybe im putting this code in the wrong place? can u explain it more thoroughly? thanks in advance :)

GMaker0507
06-28-2008, 03:44 AM
my bad "for each...in" might be for as3, i dont know

In that case, its a simple change

for (var i=0;i<_root.GamePlatforms.length;i++) {
var platform = _root.GamePlatforms[i]
// If we find one that our feet hits
if (platform.hitTest(this.Feet)) {
// true, we have found ground, and break out of this loop to save some time
foundGround=true;
break;
}
}

pachufir
06-30-2008, 05:45 AM
Thanks alot, it works great! Now i was wondering, with the code that i have right now when he hits a platform he lands on it, but if i wanted a moving platform how could i make it so that he would stay on the platform as it moved? for exapmle if a platform was moving up and down how would i make it so that once he landed on it he would travel up along with it? thanks, u've been a big help.

GMaker0507
06-30-2008, 02:49 PM
Well, moving platforms are a little different,but still simple.

Heres how i would setup moving platforms to work with the code ill show you in a sec

For Moving Platforms, i would implement a xSpeed or a ySpeed variable (Number), a baseSpeed variable(number), a direction variable (Number), Timer Variable ( Number)

Like So

NOTE: All this would go inside the moving platform

/* Add this moving platforms to the array of game platforms, youll learn how this works below.*/
_root.GamePlatforms.push (this);
// Whether its going left or right it will move at a rate of 5 pixels. increase/decrease this to make it move faster/slower
var baseSpeed:Number = 5;
// Its horizontal rate of movement
var xSpeed:Number = 0;
// Right = 1, Left = -1
var Direction:Number = 1;
// Just so we know when to reverse, increase/decrease this to make it move longer/shorter
var Timer:Number = 10;
this.onEnterFrame = function () {
xSpeed = Direction * baseSpeed;
// Countdown to zero
Timer--;
// When we hit zero, reset the timer, and switch direction
if (Timer < 0) {
Timer = 10;
Direction *= -1;
}
_x += xSpeed;
};


The i would alter my code i wrote before like so


for (var i = 0; i < _root.GamePlatforms.length; i++) {
var platform = _root.GamePlatforms[i];
// If we find one that our feet hits
if (platform.hitTest (this.Feet)) {
// true, we have found ground
foundGround = true;
// If the xSpeed variable exists
if (platform.xSpeed != null) {
//move at this platforms xSpeed
_x += platform.xSpeed;
}
// If the ySpeed variable exists
if (platform.ySpeed != null) {
//move at this platforms ySpeed
_y += platform.ySpeed;
}
//break out of this loop to save some time
break;
}
}

The reason we check if they exists is simple. We do this so we dont have to create a separte array for moving platforms. We add moving platforms into the array of regular platforms, and if we happen to land on a platform that has a x or y Speed, we move with it.

That code should work, if it doesnt i would already have a solution for it, just reply and tell me it doesnt work.

pachufir
06-30-2008, 08:33 PM
well everything works great except for one thing. on a platform thats moving up and down sometimes the character goes through the platform when it changes direction. for example if its going down then starts going up again, the character who has been standing on it will fall through it right when it starts to go up. how can i fix that? thanks.

GMaker0507
06-30-2008, 08:46 PM
do they sink through it slowly, or do they just fall through it.

Also, are you sure is the platform still hitting the feet mc. If the platform moves out of the collision against the character before the characters code can detect a collision and stop itself from falling. it will keep falling

pachufir
07-01-2008, 01:18 AM
okay i fixed the problem by making the platform slightly thicker so that the hittest is true even if he movies down one or 2 pixels.

but, unfortunately i've encountered another problem ^^;

Right now i have each level of my game in one scene. the "lives" variable is intialized in the 1st frame. the 2nd frame is the frame on which the player plays the level. the 3rd contians a "Death" screen, which allows the player to try again, by clicking a button that takes him to frame 2. the 4th frame is a game over, and the 5th is a next level frame.

this set up worked perfectly in my first scene, ie level 1. But in the 2nd scene its messed up. when i "test scene" it works perfectly, but when i "test movie" it dosnt. the button on the 2nd scene that should take me to frame 2 of the current scene (scene 2) instead takes me to the 2nd frame of the first scene.

the code i have on my button is as follows:
on(release){
_root.gotoAndPlay(2);
}

GMaker0507
07-01-2008, 02:05 AM
hmmm.. That sounds wierd :confused:

I guess, for now try different versions of that code

on(release){
_root.gotoAndPlay(2);
}

Take the '_root' out, who knows, it might work, flash acts stupid sometimes

on(release){
gotoAndPlay(2);
}

try using the the "_currentframe" property of movieclips

on(release){
gotoAndPlay(_currentframe+1);
}

Or, Try naming each frame (Start, Gameplay, Death, Game Over , or whatever you want)

then refer to their names

on(release){
gotoAndPlay("Gameplay");
}

uhh...make sure your actually on the second scene ;)

pachufir
07-01-2008, 02:22 AM
ha ha yeah, im pretty sure im on the right scene...

so how exactly do i name a frame?

GMaker0507
07-01-2008, 03:02 AM
On the timeline,select the keyframe you wish to name. Open up the properties tab ( or Window-> Properties-> Properties). There should be a text box with '<frame label>' in it. Change that to whatever you want to name the frame, if there is no actionscript on that frame, you should see a red flag on the keyframe.

Just make sure when you refer to it via 'gotoAndStop/gotoAndPlay', specifiying only one parameter, you know that it only referse to the current scene.

Meaning:

gotoAndStop("Gameplay")

can be totally different from

gotoAndStop("Gameplay","SceneName")

Also,make sure you dont have two different frames with the same name on the same scene.

pachufir
07-02-2008, 07:32 AM
yay! by using the name of the frame its working fine now... so i've finally made two levels of my game! wow... so many problems with just two levels... i can only hope it goes smoother from now on.... ^^'

Anyway, thanks alot for your help, its greatly appreciated.

GMaker0507
07-02-2008, 02:17 PM
yay! by using the name of the frame its working fine now... so i've finally made two levels of my game! wow... so many problems with just two levels... i can only hope it goes smoother from now on.... ^^'

Anyway, thanks alot for your help, its greatly appreciated.
glad i could help

Good luck with the rest of your game.

If you need any help make a new thread or send me a pm