这是我的Main类的代码:
package {
import flash.display.*;
import flash.events.Event;
public class Main extends Sprite{
private var ball:Ball;
private var paddle:Paddle;
public function Main():void {
addToUI();
beginCode();
}
public function beginCode():void {
addEventListener(Event.ENTER_FRAME, move);
addEventListener(Event.ENTER_FRAME, hitTest);
}
public function move(event:Event):void {
ball.move();
}
private function hitTest(event:Event):void {
if(ball.hitTestObject(paddle)){
vy = -20;
}
}
private function addToUI():void {
paddle = new Paddle();
ball = new Ball();
addChild(paddle);
addChild(ball);
paddle.x = 50;
paddle.y = 200;
ball.x = 75;
ball.y = 100;
}
}
}
这是我的Ball类代码:
package {
import flash.display.*;
import flash.events.Event;
public class Ball extends MovieClip {
public var vy:Number = 10;
public function move():void {
vy += 2;
this.y += vy;
}
/*
public function hitTest(paddle):void {
if(this.hitTestObject(paddle)){
vy = -20;
}
}
*/
}
}
我很难让碰撞测试正常工作。当我使用f。恩。使用
trace("Collision works")
在hitTest函数中,它会正确响应。但我无法改变vy属性。我也尝试在Ball类中编写hitTest,但编译器告诉我属性paddle是未定义的。
我该怎么做?
答案 0 :(得分:1)
您必须更改球实例的vy
:
private function hitTest(event:Event):void {
if(ball.hitTestObject(paddle)){
ball.vy = -20;
}
}