PDA

View Full Version : Problem with HitTestPoint



PeteWaugh
March 27th, 2010, 03:08 PM
Hey,
I have been having a problem with the below script.The script assumes that there is a MovieClip with the instance name "char"(I used a 32x32 square) and a MovieClip with the instance name BG on stage(I used the brush tool to create a bumpy fill/landscape roughly 1000x300.)As the script stands at the moment it moves the "BG" to create the effect that the "char" is moving along a scrolling landscape, the problem that I have is with the 'while' statement instead of stopping adding to the y value when the points are no longer touching it continues until the char is at the edge of it's hit box.If I change it so that the "char" is moving and the "BG" is stationary the script runs as expected.Can anyone tell me why this is happening and possibly offer a soloution to my problem?Many thanks!

const gravity:Number = 1;
var yspeed:Number = 0;
BG.addEventListener(Event.ENTER_FRAME, begin);
function begin(event:Event):void {
BG.y -= yspeed;
while (BG.hitTestPoint(char.x, char.y-1, true)) {
BG.y++
yspeed=0;
}

if (! BG.hitTestPoint(char.x,char.y+1,true) ) {
yspeed+=gravity;
}
}

tbo
March 28th, 2010, 03:45 PM
Here you are sir!


var temp:Number=0
while (BG.hitTestPoint(char.x, (char.y-1)-temp, true)) {
temp++;
yspeed=0;
}
BG.y+=temp;
This drove me nuts for a while.
I think in a while loop it only updates the position of what your testing against. This is why it works when you move the character rather than the BG. so the BG point never updates durring that loop even though you move it.

PeteWaugh
March 29th, 2010, 10:30 AM
Thanks very much, exactly what I was looking for !:thumb: