Skip to main content
Known Participant
December 5, 2012
Question

Using the wii accelerometer to move game characters

  • December 5, 2012
  • 1 reply
  • 736 views

I have designed a game that works well with keybaord and wiimote buttons.

Now I want to control the player using the wiimote accelerator but the player does not move smoothly. I need help pls

this is the code I have so far

package

{

import flash.events.*;

import org.wiiflash.Wiimote;

import org.wiiflash.events.*;

public class spacegame extends MovieClip

{

private var objPlayer1:player;

public const THRESHOLD:Number = 1.01;

  public const FILTER_FACTOR:Number = 0.1;

public function spacegame()

  {

createWiimote1Connection();

objPlayer1 = new player(mStage,mSprite,objShields.shieldList,objAliens,objGameTimer);

   objPlayer2 = new player1(mStage,mSprite,objShields.shieldList,objAliens,objGameTimer);

}

//connecting wiimote1 in flash

  public function createWiimote1Connection()

  {

   wiimote1 = new Wiimote();

   wiimote1.addEventListener(Event.CONNECT, onWiimote1Connect);

   wiimote1.connect();

   trace("Player 1 is connected!");

}

//wiimote1 controls the A,1,2,and Home bittons of the wiimote

  public function onWiimote1Connect(pEvent: Event):void

  {

   wiimote1.addEventListener(ButtonEvent.A_PRESS, newGame);

   wiimote1.addEventListener(ButtonEvent.A_RELEASE, onARelease);

   wiimote1.addEventListener(ButtonEvent.HOME_PRESS, backHome);

   wiimote1.addEventListener(ButtonEvent.HOME_RELEASE, onhomeRelease);

   wiimote1.addEventListener(ButtonEvent.PLUS_PRESS, begin);

   wiimote1.addEventListener(ButtonEvent.PLUS_RELEASE, onPlusRelease);

   wiimote1.addEventListener(ButtonEvent.MINUS_PRESS, restart);

   wiimote1.addEventListener(ButtonEvent.MINUS_RELEASE, onMinusRelease);

  

   //wiimote sensor eventlistener

   wiimote1.addEventListener(WiimoteEvent.UPDATE, updateWiimote1);

}

// all the event listener functions for wiimote1

  public function onARelease(pEvent:ButtonEvent):void

  {

   wiimote1.rumble = false;

  }

 

  public function onhomeRelease(pEvent:ButtonEvent):void

  {

   wiimote1.rumble = false;

  }

 

  public function onPlusRelease(pEvent:ButtonEvent):void

  {

   wiimote1.rumble = false;

  }

 

  public function onMinusRelease(pEvent:ButtonEvent):void

  {

   wiimote1.rumble = false;

  }

 

  public function updateWiimote1(pEvent:WiimoteEvent):void

  {

  

   var euLength:Number;

   var _X:Number;

   var _Y:Number;

   var _Z:Number;

  

   _X = wiimote1.sensorX;

   _Y = wiimote1.sensorY;

   _Z = wiimote1.sensorZ;

  

   trace("x is"+_X);

   trace("y is" + _Y);

   trace("z is"+_Z);

  

   //check for no motion

   euLength = Math.sqrt(_X*_X +_Y*_Y +_Z*_Z)

  

   //set force feedback for positive direction

   wiimote1.rumble = (_X>3 || _Y>3 ||_Z>3);

  

   //set force feedback for negative direction

   wiimote1.rumble = (_X<-3 || _Y<-3 ||_Z<-3);

  

   if(euLength>THRESHOLD)

   {

    objPlayer1.x +=30;

   }

  

   if(euLength<THRESHOLD)

   {

    objPlayer1.x -=30;

   }

}

}

}

This topic has been closed for replies.

1 reply

sinious
Legend
December 5, 2012

A couple things just noticed but you're moving your player by 30px per move. Unless you're running 90FPS that's a huge move so it's going to be very slow.

One way of smoothing out animation is using a really lite animation engine like TweenLite or TweenNano ( http://www.greensock.com ) and instead of moving via a set amount of pixels, have the engine continuously animate towards its target. They offer methods like "TweenLite.killTweensOf(target);" so you can continuously update the position as a moving target. The smaller duration you give to move to the ever moving target the faster the user will move there but the engine will animate the movement in more frames over time. Easing will help a bunch in there too but it needs to be used wisely (e.g. when in the middle of movement don't use "easeIn" or the character will always appear to randomly slow down and ramp up movement so only use it to start movement and then all "easeOut").

Also the speed at which updates are being received will greatly matter. Using a tween engine like above will even help with this but ultimately the event speed dictates a lot about the smoothness and responsiveness of the animation. You should try to see how many updates per second you receive to see if it's fast enough. You can use getTimer to mark the time in milliseconds when you start accepting events and check the difference between event times continuously calling getTimer and measuring the difference. That speed is really important. e.g.:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getTimer%28%29

New AS3 doc:

import flash.utils.getTimer;

import flash.events.Event;

var lastTime:uint = getTimer();

addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(e:Event):void

{

     trace("diff: " + (getTimer() - lastTime));

     lastTime = getTimer();

}

That will spray your output panel with traces on how many milliseconds exist between each frame. You'd use the same technique. Save a getTimer() time, then on each event received compare the time to a new getTimer() so you know how fast updates happen.

That leads me to my final observation which is you're using trace(). Any app running in debug mode or sending trace()s will slow down considerably. Make sure you're checking your true performance without any trace statements (omit trace in Flash Pro publish or set compiler -debug=false in Flash Builder and export release).