-1

I am using the standard Accelerometer template & am trying to create a wind effect on the ball, however I am having trouble getting my coded wind to move ball as ball only seems to respond to Accelerometer movements.

is there any way to move ball without disabling Accelerometer?

I have setup wind directions & strength, but when I test even when screen is flat & ball is in center of screen the wind variables / code has no effect.

an example of wind move ball function I am using is

        if (windD == "left")
            {ball.x -= ball.x-WindStrength;}
        if (windD == "right")
            {ball.x += ball.x+WindStrength;}

Any ideas.

Sean Carroll
  • 107
  • 1
  • 14
  • 1
    First, you either use **a += 1;** or use **a = a + 1;** - they have the same effect. You don't use both at the same time not because it is incorrect but because it is confusing. Then, if **windD == "left"**, the confusing part leads **ball.x** to always be equal to **WindStrength**. That's probably where your problem is. – Organis Feb 16 '18 at 08:58

1 Answers1

0

OK, I figured it out.

My problem was I was trying to add a wind movement function in its own function outside Accellerometer movement functions.

Solution: was to move my wind code inside accellerometer function:

ball.addEventListener(Event.ENTER_FRAME, moveBall);
function moveBall(evt:Event){
ball.x -= accelX*10;
ball.y += accelY*10;

if(ball.x > (480-ball.width/2)){
    ball.x = 480-ball.width/2;
}
if(ball.x < (0+ball.width/2)){
    ball.x = 0+ball.width/2;
}
if(ball.y > (800-ball.width/2)){
   ball.y = 800-ball.width/2;
}
if(ball.y < (0+ball.width/2)){
    ball.y = 0+ball.width/2;
}

  // Wind Functions
   //Wind Strength
    if (this.Stage_Txt_Box.text == "1")
            {WindStrength = int(2);}
    if (this.Stage_Txt_Box.text == "2")
            {WindStrength = int(3);}
    if (this.Stage_Txt_Box.text == "3")
            {WindStrength = int(4);}
    if (this.Stage_Txt_Box.text == "4")
            {WindStrength = int(5);}    
    if (this.Stage_Txt_Box.text == "5")
            {WindStrength = int(6);}
    if (this.Stage_Txt_Box.text == "6")
            {WindStrength = int(7);}
        if (this.Stage_Txt_Box.text == "7")
            {WindStrength = int(8);}
        if (this.Stage_Txt_Box.text == "8")
            {WindStrength = int(9);}            

        //Wind Directions
        trace(WindStrength);

        if (windD == "left")
            {ball.x -= WindStrength; trace("Wind is blowing left");}
        if (windD == "right")
            {ball.x += WindStrength; trace("Wind is blowing right");}
        if (windD == "up")
            {ball.y -= WindStrength; trace("Wind is blowing up");}
        if (windD == "down")
            {ball.y += WindStrength; trace("Wind is blowing down");}    
     }
Sean Carroll
  • 107
  • 1
  • 14
  • 1
    The ugly block about **WindStrength** can be replaced with a single line: **WindStrength = Math.min(9, Math.max(2, 1 + parseInt(Stage_Txt_Box.text)));** – Organis Feb 16 '18 at 15:00
  • 1
    Honestly in your place, I would just use a `Point` that defines the strength and direction at the same time, same way you would handle velocity in games. –  Feb 16 '18 at 20:17
  • Organis: Thanks Heaps :D that code worked a charm. – Sean Carroll Feb 17 '18 at 15:29