I have my script:
var radians:uint = 2;
var speed:Number = 2;
function Collision(event:Event):void{
var localpointx:Point = new Point(hero.playerbottom.x);
var heropointx:Point = hero.localToGlobal(localpointx);
var localpointy:Point = new Point (hero.playerbottom.y);
var heropointy:Point = hero.localToGlobal(localpointy);
if(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y-radians, true)){
hero.y += speed;
}
if(box1.bottomhit.hitTestPoint(heropointx.x-radians, heropointy.y, true)){
hero.x += speed;
}
if(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y+radians, true)){
hero.y -= speed;
}
if(box1.bottomhit.hitTestPoint(heropointx.x+radians, heropointy.y, true)){
hero.x -= speed;
}
}
Everything seems to be working except that the hit script is too laggy in my opinion, when he touches the surface it pushes him back at the number of speed and if you walk towards the item he is being pushed back and walks to the item at the same time; walking up a slope will look like walking up stairs. I just want him to not being able to go through the item and that it gives a smooth collision.
If you don't understand what i mean with the laggy script, create an own swf file and try it out by yourself.
Thanks in advance ![]()
I already have 60 fps, i changed the value to 120 but there was still a minor lag in the movement.
What i am trying to achieve here is normal hittest where one movieclip is the wall the floor or a slope. I searched for a solution for this and all i can seem to find is the hitTestPoint solution, nothing about what happens afterwards. How the script should manage to make the movieclip to act as a wall. Like in a platformer.
I am thankful that you try to help me but this did not fix the problem, i think the major problem in this is that when the player touches the item he gets pushed back for too many frames, the good thing would be to have hero.x or y to be -- or ++ instead of += speed. But if i do it the amount of the speed that the player is moving is higher than the pushback and he is actually managing to get through it. :/
So the player is not being pushed back by the correct amount? Sorry if I'm misunderstanding here haha I'm terrible at reading other people's code.
If that is the case you could simply set the hero's x/y positions to be where you want him to be after the collision instead of adding/subtracting speed:
e.g. if(hero.y + hero.height > floorOfLevel_mc.y)
{
hero.y = floorOfLevel_mc.y - hero.height;
}
if that makes any sense? Sorry again if this isn't what you're meaning.
I was looking around i saw that "while" the loop condition could fix the problem and i changed everything to:
function Collision(event:Event):void{
var localpointx:Point = new Point(hero.playerbottom.x);
var heropointx:Point = hero.localToGlobal(localpointx);
var localpointy:Point = new Point (hero.playerbottom.y);
var heropointy:Point = hero.localToGlobal(localpointy);
while(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y-radians, true)){
hero.y += speed;
}
while(box1.bottomhit.hitTestPoint(heropointx.x-radians, heropointy.y, true)){
hero.x += speed;
}
while(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y+radians, true)){
hero.y -= speed;
}
while(box1.bottomhit.hitTestPoint(heropointx.x+radians, heropointy.y, true)){
hero.x -= speed;
}
}
It all works fine until it touches it, then everything crashes and after 15 secs the session closes. Isn't it possible to have a "while" in a ENTER_FRAME function? If not, how am i supposed to check if the player touches it?
After a little bit of testing i realized that even this code made it crash:
var localpointx:Point = new Point(hero.playerbottom.x);
var heropointx:Point = hero.localToGlobal(localpointx);
var localpointy:Point = new Point (hero.playerbottom.y);
var heropointy:Point = hero.localToGlobal(localpointy); while(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y-radians, true)){
hero.y += speed;
}
It didn't even have to be in a function to crash, was it the nested movieclip or the variablepoints that made it go all crazy and crash? What is the actual problem here? The only thing i know is that it is possible to have a "while" with a hitTestPoint.
Thanks in advance ![]()
Firstly your test condition does not change during the while loop so if it's true when the loop starts it continue to execute until it finally crashes. To fix this you have to revalue the var used in the test on each loop iteration. However... the next point:
Secondly the screen redraw happens when the playhead enters the new frame, and the playhead enters the new frame only after all the code is executed in the current frame. This means you do not see any visual movements while while loop is running.
So, if you are testing the condition on every frame you should change while to if, so that the test runs once per frame.
But when i choose if in the hittest function it only adds 1 movement on the y axis each frame, i want to add all the movement within one frame wich can be done with "while" that is why i want it, heres an example that it does work: http://www.newgrounds.com/bbs/topic/1033481
My problem is that i have nested movieclips. ![]()
Your code before the loop is not right either
Not sure exactly what you're after but purely from the code point of view here's the corrected:
while (box1.bottomhit.hitTestPoint(hero.localToGlobal(new Point(hero.playerbottom.x, hero.playerbottom.y)).x, hero.localToGlobal(new Point(hero.playerbottom.x, hero.playerbottom.y)).y - radians, true)) {
hero.y += speed;
}
North America
Europe, Middle East and Africa
Asia Pacific