经与三角试图给方向球的麻烦麻烦、方向

2023-09-08 14:31:05 作者:都已過去,何必再忆

哦,上帝,你们我很抱歉再次打扰,我从来没有想到,开始编写AS3将是这款硬盘(在容易一开始,但现在我的心被阻止),我仍然无法使球跟随鼠标在所需的速度和角度,我把JETT的例子,但也许我不会实施是正确的,这里是code;我将解释什么是我想要完成

Oh God , guys I’m sorry to be bothering again, I never imagined that start coding AS3 would be this hard (was easier at the very beginning but my mind is blocked now), I’m still unable to make the ball follow the mouse at the speed and angle desired, I took jett’s example but probably I’m not implementing it right, here is the code ; I’ll be explaining what I’ trying to accomplish

import flash.geom.Point;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
// here I added a custom cursor to my mouse (it’s an aim in png format)
var cursor:MovieClip;
function initializeGame():void
{
cursor = new Cursor();
addChild(cursor);
cursor.enabled = false;
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);

}

function dragCursor(event:MouseEvent):void
{
cursor.x = this.mouseX;
cursor.y = this.mouseY;
}

initializeGame();
var mouse=this.Mouse;

// here I want to make the player  (called kicker) to kick the ball
stage.addEventListener(MouseEvent.CLICK, kick);

function kick(evt:Event){

kicker_mc.play(); // here I animate the kicker movieClip

this.addEventListener(Event.ADDED, moveBall);// this is the function that will move     the ball towards the goal

}

//And here unsuccessfully trying to make the ball start moving to the cursor position,     (currently when I kick the ball it appears at the right upper corner of the swf, exactly where the cursor appears when movie is tested

var speed:Number;
var angle:Number;
speed=200;
angle = Math.atan2(mouse.y-bola.y, mouse.x-bola.x);

function moveBall (event:Event){

ball.x += Math.cos (angle) * speed;
ball.y += Math.sin (angle) * speed;


}

我再次就真的AP preciate你的指导家伙

Once again I’ll really appreciate your guidance guys

我开始创建与AS3一个足球点球大战的比赛,但我无法把球给速度和方向,一旦它被踢,我无法让玩家打完球(是的,我不是开发人员,但我一直在努力地完成比赛),所以我设法把一个延迟的功能,让我把球移动到目标3.5秒之后,当玩家的动画模拟踢,这是code:

I Started creating a Soccer Penalty Shootout game with AS3 but I’m having trouble giving speed and direction to the ball once it is kicked, I was unable to make the player kick the ball (yes I’m not a developer but I’ve been trying hard to complete the game ) so I managed to put a delay on a function which allows me to move the ball to the goal 3.5 seconds later while the animation of the player simulates the kick, this is the code:

kick_btn.addEventListener(MouseEvent.CLICK, kick);

function kick(evt:Event)
{
    kicker_mc.play();

    new delayedFunctionCall (myFunctionToStartLater, 350);

    function myFunctionToStartLater():void
    {    
        ballkicked();
    }
}

function ballKicked(event:Event)
{
    ball_mc.y=180;
}

现在检查我只是转移球到不同的位置,但我想把它扔在功能门将ballkicked();这里就是我想给真正的运动球这样的拍摄可以是一个目标,我知道我需要使用一些三角给的方向和速度,但三角似乎是很复杂的,我下面的教程,但我只能prepare一些变量和parameteres但后来我迷路了:

Now check that I’m only moving the ball to a different position but I want to throw it to the goalkeeper in the function ballkicked(); here’s is where I want to give real movement to the ball so the shoot can be a goal, I know I would need to use some trigonometry to give direction and speed but trigonometry seems to be very complicated, I was following a tutorial but I was only able to prepare some variables and parameteres but then I got lost:

var xSpeed:Number;
var ySpeed:Number;
var angle:Number;
var speed:Number;

这应该去功能:

angle = this.rotation / 180 * Math.PI;
xSpeed = Math.cos(angle) * speed;
ySpeed = Math.sin(angle) * speed;

我会真的AP preciate你的耐心和这方面的帮助,我真的想学习如何做的事情,我不想放弃。非常感谢

I will really appreciate your patience and help with this, I really want to learn how to do things and I don’t want to give up. Thanks a lot

推荐答案

atan2函会给你的角度,以扔的球。要做到这一点,你需要两个位置向量,比如我们会用鼠标位置,球的位置。

The atan2 function will give you the angle to "throw" ball. To do this, you will need two position vectors, for example we'll use the mouse position to ball position.

angle = atan2(mouse.y-ball.y, mouse.x-ball.x)

的通知使y COORDS是第一次使用。这会给你一个数字,从中可以向前进步的球具:

notice that y coords are used first. This will give you a number from which you can progress the ball forward with:

ball.x += cos(angle)*speed
ball.y += sin(angle)*speed

这会使球走向哪里鼠标。然而,你将需要考虑重力通过弯曲的角度更接近地面。当接地被击中,你可以简单地降低速度和翻转向量的y。

this will make the ball move toward where the mouse is. However you will need to take into account gravity by curving the angles closer to ground. When the ground is hit you can simply decrease speed and flip the y of vector.

*的您可能会发现使用一个额外的加速度矢量是有用的。 * 的 有了这个,你可以设置的加速度矢量类似于我上面显示:

*You may find using an additional acceleration vector useful. * With this you could set the acceleration vector similarly to what I showed above:

//same as above
ball.accx += cos(angle)*speed
ball.accy += sin(angle)*speed

开始。然后,眼前这个载体添加到您的POS载体上的每一帧。你最初的框架后,您将需要提供某种形式的阻尼,并一本正经地这是给你,你想怎么来实现。

initially. Then just add this vector to your pos vector on every frame. After your initial frame you will want to provide some sort of damping and gravity- this is up to you how you would like to implement.

来计算此的另一种方式是使用一个正弦波来说明的路径。你可以看到一个例子在这里:正弦波,这是比较容易开始,但你需要学习向量的概念来处理冲突,如我上面显示。

An alternative way to compute this is to use a sine wave to illustrate the path. You can see an example here: sinewave, this is easier initially but you will need to learn the concepts of vectors to deal with collisions such as what I showed above.