-1

I don't wanna to overlap each other the objects.Also I wanna keep the objects in stage limit.The buttons must move away when hit each other.I tried hitTestObject but buttons move like this.

Sample move code for fish 2 *UPDATE

var fish2x:Number=10;
var fish2y:Number=14;
 
stage.addEventListener(Event.ENTER_FRAME,h42);
function h42(s:Event = null) {
fish2.x+=fish2x;
fish2.y+=fish2y;
if ((fish2.x>=stage.stageWidth-fish2.width/2)|| (fish2.x <= fish2.width/2 )) {
    fish2x*=-1;
}
if ((fish2.y>=stage.stageHeight-fish2.height/2)|| (fish2.y <= fish2.height/2 )) {
    fish2y*=-1;
}

if (fish2.hitTestObject(fish3)){
fish2y *= -1;
fish3y *= -1;
h42();
}
}

Also I tried in diffrent function

stage.addEventListener(Event.ENTER_FRAME,crash);
function crash(s:Event) {
 
if (fish2.hitTestObject(fish || fish3 )) {

fish2y*=-1;
message.text="crash";
}
}

For more than 2 fish not work. I set null fish2 and fish 3 than I use this code.

if (fish2.hitTestObject(fish3 || fish4)){
fish2y *= -1;
fish2x *= -1;
h42();
 }

I changed hittestoject all off them.All function change like this but it not work.

Update 2

Now it's no error,but not happens when fish3 hit each other.I removed "null" fish and fish 3 just used for fish 2.

        if (fish2.hitTestObject(fish || fsih3)){
fish2y *= -1;
fish2x *= -1;
fishy*=-1;
fishx*=-1;
fish3y*=-1;
fish3x*=-1;
}
}
KucuKeko
  • 111
  • 8
  • "Move like crazy" doesn't help us visualize what is actually happening. – Neal Davis Feb 10 '17 at 20:45
  • I don't know if you can do hittestobject like that (x || b). I think you may need to do hittestobject(x) || hittestobject(b) – Neal Davis Feb 10 '17 at 20:47
  • Hittestobject work correct I can see the message text when hit each other.I was use " | | " I got a 7 object on the stage just test for 2 fish.İf fish 2 hit the fish or fish3 go reverse direction. My problem is How do I move the object to reverse direction when hit each other. – KucuKeko Feb 11 '17 at 09:19
  • You are only trying to get them to reverse direction in the y direction? That's what it looks like. What happens instead? I repeat: "move like crazy" tells us *nothing* – Neal Davis Feb 11 '17 at 09:30
  • I add a gif image link on first message.I test first y.I was test x until see how is working.I think I explain clearly what I need.The buttons must move reverse direction when hit each other.for example: if Fish 2 hit below or top from to fish 3,then fish 2 go y.if Fish 2 hit to fish 3 from left or right side then fish 2 go x. – KucuKeko Feb 11 '17 at 10:23

1 Answers1

1

I think it is because they are both moving. When you check collisions between A and B Fishes, if the collision is true, don't just change their speed by *=-1. Instead, also move them one time.

if (A.hitTestObject(B)){
    Ay *= -1;
    Ax *= -1;
    By *= -1;
    Bx *= -1;
    h42();
}

and add null to your default value like this:

function h42(s:Event = null) {
Neal Davis
  • 2,010
  • 2
  • 12
  • 25
  • I change function to null and add in h42 function to your code.But it's moving stuck again.What do you suggest ? Thanks – KucuKeko Feb 11 '17 at 17:49
  • Ok now not stuck but it's jump another position.Here is the gif http://keko11.byethost24.com/bandituruk.gif – KucuKeko Feb 11 '17 at 18:20
  • Can you paste the code in to your original post as it is currently? – Neal Davis Feb 11 '17 at 18:24
  • You are only changing their y direction? That doesn't make sense. Got to change them both. X and y. – Neal Davis Feb 12 '17 at 00:20
  • Keep in mind this is NOT even approximating any sort of physics based behavior. If you want to have them change direction based on what PART of the fish collided (right edge or top edge or left edge etc) then you've got more work ahead of you. That is much more complex. But to simply have them reverse direction you just need to address both y and x directions. – Neal Davis Feb 12 '17 at 00:46
  • Neal it's work for the two fish and I have 8 object on the stage when I more than 2 fish it's stop I get timeline error.I updated first message what I tried. – KucuKeko Feb 12 '17 at 10:16
  • You have a typo. Fsih3 instead of Fish3 – Neal Davis Feb 12 '17 at 16:32
  • Bout you'll still have a problem because what you're telling it to do is to reverse ALL fish direction if fish 2 hits either fish. Which will be odd for the fish which didn't get hit to suddenly reverse direction along with the two that did get hit. – Neal Davis Feb 12 '17 at 16:35
  • Make a different hittestobject for each Pair of fish. Don't use the || operant. – Neal Davis Feb 12 '17 at 18:32
  • Solved.I created new function and use all hittestobject in one function.Thank you. – KucuKeko Feb 12 '17 at 22:05