2016-04-24 10 views
0

私はボールとプレイヤーを持っています。プレイヤーがボールを打つ方向にボールを移動するにはどうすればよいですか?プレイヤーとボールのxとyを比較しようとしました。しかし、それは動作しませんでした。あなたはこれまでにしようとしているものをいくつかのコードをポストする必要がありオブジェクトをどの方向に動かすか(as3)?

答えて

0

あなたはボールの移動方向であるべきかを決定することができます条件ならば...

は、私はいくつかと思います。これは、それが動作するためにあなたを助ける

if(player.hitTestObject(ball)) { 
//if player hit the ball 
//now you should see the possition of ball and of the player 

//[we need some variables, you will see for what later on] 
var x_direction, y_direction; 

if(player.x < ball.x) { 
    //that means your ball is in the right , and player in the left 
    //and the ball should change position x with '+' sign --> (you will use this a bit latter when you will move the ball) 
    //now we will use the x_direction variable to retain the direction in which the ball should move 
    //because player is in the left (player.x < bal.x) that means the ball should move to right 

    x_direction = 'right'; 
} 

else { 
    //it's opposite .... 
    //because player is in the right (player.x > bal.x) that means the ball should move to left 

    x_direction = 'left'; 
} 

//now we finished with x...we should check also the y axis 

if(player.y < ball.y) { 
    //that means your ball is below the player 
    //player hit the ball from the top 

    y_direction = 'down'; 
} 

else { 
    //it's opposite .... 
    //the ball should go up 
    y_direction = 'up'; 
} 


// now we have to move the ball 
// we do this according to possition we checked before 

if(x_direction == 'right') { 
    //you should define your speed somewhere up like var speed = 10; 

    ball.x = ball.x + speed; // remember the sign '+'? 
    //or ball.x += speed; 
} else { 
    ball.x -= speed; 
} 

//and the same with y direction 
if(y_direction == 'down') { 
    //you should define your speed somewhere up like var speed = 10; 

    ball.y += speed; 
} else { 
    ball.y -= speed; 
} 

// you can put this movement in an enterframe loop and make the movement.... 

}

希望...

:私はあなたにヒントを与えるだろう
関連する問題