2017-06-21 66 views
0

我一直在關注如何在as3中製作一個簡單的高爾夫球遊戲的教程。這個球應該是,當我走動我的手機移動,但由於某種原因,它不會: Golf game image球不移動AS3

這裏是我的外部代碼至今:

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() 

回答

0

我做了一個簡單的測試出你的代碼,並去除了所有不必要的東西幀腳本,它爲我工作:

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

所以很可能問題不在於代碼,而在於您測試它的方式。

+0

感謝您的評論!我已經移動了一些代碼並修復了它們被放置的方式,並確定它已修復它! – Nat