Using the wii accelerometer to move game characters
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;
}
}
}
}