Reputation: 337
As the title to my question implies, I'm building a 2d platform game. I have my code set up so that when the character hits the ground gravity discontinues to pull on the character. Now, although that will keep the character from falling through the ground, it does not stop the character exactly at the top of the platform. There for I tried using this solution:
if(ground.hitTestPoint(left_foot_point.x,left_foot_point.y,true)||ground.hitTestPoint(right_foot_point.x,right_foot_point.y,true)){
Loop: for(var i:int=0;i<1000;i++){
if(ground.hitTestPoint(left_foot_point.x,left_foot_point.y,true)||ground.hitTestPoint(right_foot_point.x,right_foot_point.y,true)){
char.y-=1;
}else{
char.y+=1;
break Loop;
}
}
}
The goal of this code was to pull the character out of the ground and then set him down so that he is just barely touching the ground. Unfortunately this code doesn't work and sends the character flying into the sky. The code seems to refuse to acknowledge when the character is no longer touching the ground. Any one have an idea of what I'm doing wrong here?
Upvotes: 0
Views: 62
Reputation: 18351
To enter your for loop, this condition must be true:
ground.hitTestPoint(left_foot_point.x,left_foot_point.y,true) ||
ground.hitTestPoint(right_foot_point.x,right_foot_point.y,true)
In your for loop, you only break if that same condition is not true. As it was true before, and we aren't modifying left_foot_point
, right_foot_point
, or ground
anywhere, it will continue to be true for the entirety of the loop. Thus, the loop will run all 1000 iterations and the character will be moved up 1000 pixels every time.
Upvotes: 1