球不动AS3

时间:2017-06-21 09:15:45

标签: actionscript-3 flash

我一直在关注如何在as3中制作简单的高尔夫游戏的教程。当我移动手机时球应该移动,但出于某种原因它不会: Golf game image

到目前为止,这是我的外部代码:

package  {
import flash.events.Event;
public class golf extends MovieClip {

    public function golf() {
        stop();
        startbtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextScene);
        fl_Accelerometer.addEventListener(AccelerometerEvent.UPDATE, fl_AccelerometerUpdateHandler);
        ball.addEventListener(Event.ENTER_FRAME, moveBall);
        ball.addEventListener(Event.ENTER_FRAME, targethit);

    }

    var accelX:Number;
    var accelY:Number;
    var fl_Accelerometer:Accelerometer = new Accelerometer();

    function fl_AccelerometerUpdateHandler(event:AccelerometerEvent):void
    {
        accelX = event.accelerationX;
        accelY = event.accelerationY;
    }

    function moveBall(evt:Event){
        ball.x -= accelX*30;
        ball.y += accelY*30;

    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;
    }
        }


    function targethit(e:Event):void {
        if (ball.hitTestObject(hole1)) {
            gotoAndStop(1, "End");
        }
    }


    function fl_ClickToGoToNextScene(event:MouseEvent):void
        {
        MovieClip(this.root).nextScene();
        }



}

}

到目前为止出现的错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at accelerometer()[C:\Users\HP\Downloads\GOLF\accelerometer.as:14]
    at runtime::ContentPlayer/loadInitialContent()
    at runtime::ContentPlayer/playRawContent()
    at runtime::ContentPlayer/playContent()
    at runtime::AppRunner/run()
    at ADLAppEntry/run()
    at global/runtime::ADLEntry()

1 个答案:

答案 0 :(得分:1)

我用你的代码做了一个简单的测试,一个包含所有不必要的东西的框架脚本,它适用于我:

import flash.events.Event;

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

var accelX:Number = 0;
var accelY:Number = 1;
var flAxx:Accelerometer = new Accelerometer;

flAxx.addEventListener(AccelerometerEvent.UPDATE, onAxxel);

addEventListener(Event.ENTER_FRAME, onFrame);

function onAxxel(e:AccelerometerEvent):void
{
    accelX = e.accelerationX;
    accelY = e.accelerationY;
}

function onFrame(e:Event):void
{
    ball.x -= accelX * 30;
    ball.y += accelY * 30;

    ball.x = Math.max(ball.x, ball.width / 2);
    ball.x = Math.min(ball.x, stage.stageWidth - ball.width / 2);

    ball.y = Math.max(ball.y, ball.width / 2);
    ball.y = Math.min(ball.y, stage.stageHeight - ball.width / 2);
}

因此很可能问题不在于代码,而在于您测试它的方式。