天啊,不好意思又来打扰了,没想到开始写AS3会这么难(刚开始还容易,现在脑子都被堵住了),还是没办法让球跟上鼠标以所需的速度和角度,我以杰特的例子为例,但可能我没有正确实施,这是代码;我会解释我想要完成的事情
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;
}
再次感谢您的指导
老问题
我开始使用 AS3 创建足球点球大战游戏,但是一旦球被踢出,我无法给球提供速度和方向,我无法让球员踢球(是的,我不是开发人员,但我已经一直在努力完成游戏)所以我设法延迟了一个函数,该函数允许我在 3.5 秒后将球移动到球门,同时玩家的动画模拟踢球,这是代码:
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() 中将球扔给守门员;这是我想给球真正运动的地方,所以射门可以成为一个目标,我知道我需要使用一些三角函数来给出方向和速度,但三角函数似乎非常复杂,我正在学习一个教程,但我是只能准备一些变量和参数,但后来我迷路了:
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;
我非常感谢您的耐心和帮助,我真的很想学习如何做事,我不想放弃。非常感谢