• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Collision problem

Community Beginner ,
May 15, 2011 May 15, 2011

Copy link to clipboard

Copied

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

TOPICS
ActionScript

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 18, 2011 May 18, 2011

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;

}

Votes

Translate

Translate
Guest
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

All I can think of doing is increasing the framerate of your project (you'll need to decrease your speed variable accordingly). I assume your collision function is being called on ENTER_FRAME events?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

I forgot to mention that this is done in an Event.ENTER_FRAME function.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

anyone?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

You could try having your collision function called from a Timer running at very small intervals rather than on an enter frame event. That's about all I've got to offer just now...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

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. 😕

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

But the thing is, i don't want to change the hittest, just the thing afterwards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 16, 2011 May 16, 2011

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 17, 2011 May 17, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2011 May 18, 2011

Copy link to clipboard

Copied

It will definitely crash because once it is evaluated true your while loop will never exit. The logic is wrong anyway but do not use while to move objects in general if you running this code on every frame then you can use if instead

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 18, 2011 May 18, 2011

Copy link to clipboard

Copied

So there is no way i can make the while statement true when it is touching the item and it isn't, so i can't do:


while(Boolean == true){

hero.y ++;

}

?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2011 May 18, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 18, 2011 May 18, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2011 May 18, 2011

Copy link to clipboard

Copied

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;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 18, 2011 May 18, 2011

Copy link to clipboard

Copied

LATEST

Thanks it worked perfectly! You certainly made my day

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines