PDA

View Full Version : [AS2] ladders for a game (and an array)


hansolo
11-09-2010, 11:45 AM
I know this no the best post. im not actualy expecting a response
How ever if some one has had experience with using ladders in a platform game. As this way could be possible. if i could say if touch ladder_one or ladder two hittest == true for both values not just 1

i dont think i can use an array. with the example below.
of the variable(player_on_ladder checks wether the player has touched a ladder.
Now the problem is the player is touching the first ladder, however is not touching the second ladder.
which returns
This fine when i want the player to climb the ladder. however i want to make the player when not touching the ladder
fall towards the ground. So created both variable to check wether the player has touched the ladder and
then is not touching the ladder. When the player touches the ladder the loop checks and returns ladder one has touched(true)
and Ladder_two not touched and the value is false.
as true and false. This is correct



var ladder = new Array(ladder_one,ladder_two);
var player_on_ladder:Boolean = false;
var ladderTouches:Number = 0;


player.onEnterFrame = function(){

for(var i=0; i < ladder.length; i ++){
// climbs the ladder if player on ladder
if(player.hitTest(ladder[i])){
player_on_ladder = true;
textLadder.text = player_on_ladder;
trace("hit =" + ladder[i]);
ladderTouches = 1;

// problem which is correct //output panel .
// value returned true and false table_one is touched and table_two is not touched.
// making the variable
}else if(!player.hitTest(ladder[i])){
player_on_ladder = false;
textLadder.text = player_on_ladder;
trace(player_on_ladder);
}
// make player go up the ladder when touching the ladder
if(Key.isDown(Key.UP)and player_on_ladder == true){
player._y -=5;
}
// make player climb down the ladder when touching the ladder
if(Key.isDown(Key.DOWN)and player_on_ladder == true){
player._y +=5;
//player not on ladder

//__________________________________________________ ____player fall when after touching and decides to leave halfway point________
}else if(player_on_ladder == false and ladderTouches == 1){
player._y +=3;
// now here lie my problem as the output panel is showing.
// this correct however this why cant work at the moment.


}
}
}

bluemagica
11-09-2010, 01:06 PM
sigh.....just exit the loop when you are on a ladder, that is as soon as player_on_ladder becomes true. He cannot be on multiple ladders at the same time, so there is no point in checking further.

neilmmm
11-09-2010, 03:59 PM
try http://www.emanueleferonato.com/2008/09/23/new-tile-based-platform-engine-part-6-ladders/

hansolo
11-09-2010, 08:23 PM
cheers for that neil.

i still not done in in a tile engine i keep avoiding it.so i think by next week im gonna have to just go with trying to create a game in tile egine world. oh definitley my next game attempt. ive used movieclip to knock in differnet ways to knock em off.


my method is inaccurate though. cheers

hansolo
11-09-2010, 08:25 PM
sorry blue magica

ive never exited a loop before. I didnt know ii could.

can you show me an example how to exit the loop.


sigh.....just exit the loop when you are on a ladder, that is as soon as player_on_ladder becomes true. He cannot be on multiple ladders at the same time, so there is no point in checking further.

rrh
11-09-2010, 09:12 PM
You can use "break" to exit a loop.

for (loop whatever) {
if (something happens)
break;
}


If you weren't familiar with "break", you could still get by with something like this:

dontbreak=true;
for (i=0;i<ladderlist.length AND dontbreak;i++) {
if (ladderhit)
dontbreak=false;
}

Or, not as efficient as breaking, but it was something I would use a lot when I was younger:
ladderfound = false;
for (each ladder in ladderlist) {
if (ladderhit)
ladderfound=true;
}

if (ladderfound==true) {
...move on ladder...
} else {
..fall down...
}

hansolo
11-10-2010, 09:34 AM
when i run this code trace yes does not appear. And its only stops running the trace (no) when i hit the firstobject in the Array not on the second or third obect..


var blocks = new Array(box_two, box_one, box_three);

player.onEnterFrame = function(){

for (i = 0; i < blocks.length; i++) {
if (player.hitTest(blocks[i])) {
break;
trace(" yes ");
trace(blocks[i]);


}else if(!player.hitTest(blocks[i])){
trace(" no ");



}





your code



for (loop whatever) {

if (something happens)// should the be handler here

break;


}

bluemagica
11-10-2010, 12:23 PM
Man, do you even think before you code? if you break "before" trace(" yes "); how can you expect your program to execute that?


var blocks = new Array(box_two, box_one, box_three);
player.onEnterFrame = function()
{
for (i = 0; i < blocks.length; i++)
{
if (player.hitTest(blocks[i]))
{
trace(" yes ");
trace(blocks[i]);
break;
}
else if (!player.hitTest(blocks[i]))
{
trace(" no ");
}
}
}


And another thing, why the heck are you doing a !hitTest in the else? if the "if" hitTest is false and it reaches the else, it naturally means there was no collision...

hansolo
11-11-2010, 10:17 AM
ok cheers for that. I do think but i am making mistakes. how ever by you pointing them out i can rectify my mistakes.

so i thank you

I wasnt sure what the break statement does. I thought the trace statement would have ran through.now i know it wont if a break before the trace. thank you


var blocks = new Array(box_two, box_one, box_three);
player.onEnterFrame = function(){

for (i = 0; i < blocks.length; i++){

if (player.hitTest(blocks[i])){

trace(" yes ");
trace(blocks[i]);

} else{

trace(" no ");

}