Skip to main content
rworsl
Participant
June 19, 2014
Question

Accelerometer function issue

  • June 19, 2014
  • 2 replies
  • 682 views

Hi guys,

I'm very new to flash and AS3 and I'm getting an annoying issue. I'm currently trying to make a simple game using accelerometer controls, and it works fine for the first level, but as soon as it tries to jump to either another scene or frame 2 (I've tried having the different levels on either scenes or frames, and the error occurs regardless) I'm getting this error: TypeError: Error #1009: Cannot access a property or method of a null object reference.

From looking around on the web I can see that it's trying to load something that's not there, but I'm not sure at what point it's stopped existing. Do I need to terminate the function at the end of the 1st scene and then re-initialize?


Here is the first frame's code, the error message is: "TypeError: Error #1009: Cannot access a property or method of a null object reference. at ball_game_fla::MainTimeline/onAccUpdate()[ball_game_fla.MainTimeline::frame1:17]"


import flash.sensors.Accelerometer;import flash.events.AccelerometerEvent;

import flash.utils.Timer;

import flash.events.TimerEvent;

import flash.events.Event;

var isFalling:Boolean = false;

var theAcc: Accelerometer = new Accelerometer();

  theAcc.setRequestedUpdateInterval(10);

  theAcc.addEventListener(AccelerometerEvent.UPDATE,onAccUpdate);

stop();

function onAccUpdate(e:AccelerometerEvent)

  {

  ball.x -= (e.accelerationX * 10);

  ball.y += (e.accelerationY * 10);

  dummyBall.x = ball.x;

  dummyBall.y = ball.y;

  ballShadow.x = ball.x + 5;

  ballShadow.y = ball.y + 5;

  

  if (ball.x < 0){

  ball.x = 0;

  

  }else if (ball.x > stage.stageWidth){

  ball.x = stage.stageWidth;

  }

  if (ball.y < 0){

  ball.y = 0;

  

  }else if (ball.y > stage.stageHeight){

  ball.y = stage.stageHeight;

  }

  

  if(dummyBall.hitTestPoint ( hole.x, hole.y, true ) && isFalling == false){

  isFalling = true;

  hole.gotoAndPlay(2);

  ball.visible = false;

  ballShadow.visible = false;

  dispatchEvent(new Event("finished", true));

  

  }

}

Any pointers would be really useful

This topic has been closed for replies.

2 replies

June 20, 2014

It looks like you are accessing the ball from an instance name. Have you set the ball to the same name in the next scene?

Inspiring
June 19, 2014

Odd line 16 works. I would try tracing the event data to see if accelerationY is not null or something. Also, as you'll find out - just don't use scenes at all. Never. Frames are fine, but still not as good as just pure AS with no frames, which is what I believe most do. Anyway, just noticed your update interval is 10ms - that is too fast. That's asking for 100 updates per second... way more than you need. Try 100 or so and see if that helps.

rworsl
rworslAuthor
Participant
June 19, 2014

Thanks for the reply dmennenoh, I'll adjust the values and see what happens. I've tried it with both the second level on a different scene and with it on the main timeline's second frame too, both produce the same error. Good advice though, i'll avoid scenes like the plague from now on!