Author Topic: Problem with movement  (Read 679 times)

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Problem with movement
« on: Sun, Mar 3, 2013 »
Hi,

I'm having a little problem with the movement of my sprites. The thing is, the sprite can't move horizontally unless its jumping. I have a separate class to control the sprite movements and I'm using FlxControl. When I use Flxcontrol inside of the sprite itself, there is no problem but when I use my ControlHandler class(which is using Flxcontrol) then the problem appears. I'm pasting my code here:
Code: [Select]

Here's my Controller class:
package Tristan
{
import flash.utils.Dictionary;
import org.flixel.FlxG;
import org.flixel.plugin.photonstorm.*;
import org.flixel.plugin.photonstorm.FlxControl;
import org.flixel.FlxObject;

/**
* ...
* @author Tristan Angeles
*/
public class ControlHandler
{

private static var _instance:ControlHandler;
private var controlDict:Dictionary;



//controller types
public static  var ARROWKEYS:uint = 0;
public static var WASD:uint = 1;
public static var HJKL:uint = 2;
//public static var typesArray:Array = [0, 1,2];

//controller array
private var controllerArray:Vector.<FlxControlHandler>;


//movement
private var xSpeed:uint=0;
private var ySpeed:uint=0;
private var maxSpeedX:uint = 0;
private var maxSpeedy:uint = 0;
private var xDecel:uint = 0;
private var yDecel:uint = 0;
private var jumpHeight:uint = 0;
private var jumpRepeat:uint = 0;
private var gravity:uint = 0;
private var fall:uint = 0;


public static function Instance():ControlHandler
{
if (_instance == null)
{
_instance = new ControlHandler();
trace("ControlHandler instantiated");

}

return _instance;
}




public function ControlHandler()
{
if (FlxG.getPlugin(FlxControl) == null)
{
FlxG.addPlugin(new FlxControl);
}

controllerArray = new Vector.<FlxControlHandler>;
controlDict = new Dictionary();


}




public function AddControl(numPlayer:uint):void
{
//check if there is an instance of game handler
if (!GameHandler.Instance()) throw new Error("GameHandler not instantiated");
trace(numPlayer);


//get player from game handler and get control type
var player:Player = GameHandler.Instance().GetPlayer(numPlayer);
var type:uint = player.ctrlType;
var controllerNumber:uint = player.controllerNumber;

//create an instance of Flxcontrol handler
var controller:FlxControlHandler=FlxControl.create(player.currentShifter,
FlxControlHandler.MOVEMENT_ACCELERATES, FlxControlHandler.STOPPING_DECELERATES, controllerNumber, true, false);





//add controller to controllerArray
controllerArray.push(controller);

if (controllerArray.length != 0)
{
setControlStats(player.ctrlType,controller);
}

controlDict[controller] = player.PlayerID;









}

public function GetPlayerController(playerID:String):FlxControlHandler
{
return controlDict[playerID];
}



public function setControlStats(playerNum:uint,controller:FlxControlHandler):void
{
//set FlxControlHandler to player's control type
switch(playerNum)
{
case ARROWKEYS:
controller.setCursorControl(false, false, true, true);
controller.setJumpButton("UP", FlxControlHandler.KEYMODE_PRESSED,jumpHeight,FlxObject.FLOOR, jumpRepeat,fall);

break;

case WASD:
controller.setWASDControl(false, false, true, true);
controller.setJumpButton("W", FlxControlHandler.KEYMODE_PRESSED,jumpHeight, FlxObject.FLOOR, jumpRepeat,fall);

break;
case HJKL:
controller.setHJKLControl(false, false, true, true);
break;
}

controller.setMovementSpeed(xSpeed, ySpeed, maxSpeedX, maxSpeedy,xDecel,yDecel);
controller.setGravity(0, gravity);
FlxControl.start();
}




public function ShiftControl(player1:Player, player2:Player):void
{

FlxControl.clear();

//switch current shifters
var temp:Shifter = player1.currentShifter;
player1.currentShifter = player2.currentShifter;
player2.currentShifter = temp;





//then set those shifers to players current control
var ctrl1:FlxControlHandler = FlxControl.create(player1.currentShifter,
FlxControlHandler.MOVEMENT_ACCELERATES, FlxControlHandler.STOPPING_DECELERATES, player1.controllerNumber, true, false);
setControlStats(player1.ctrlType, ctrl1);


var ctrl2:FlxControlHandler = FlxControl.create(player2.currentShifter, FlxControlHandler.MOVEMENT_ACCELERATES,
FlxControlHandler.STOPPING_DECELERATES, player2.controllerNumber, true, false);
setControlStats(player2.ctrlType, ctrl2);

//flicker effects
FlxG.flash(0xfff);
player1.currentShifter.flicker(5);
player2.currentShifter.flicker(5);




}

public function InitializeController():void
{
xSpeed= GameHandler.gameXml.movement.@xSpeed;
ySpeed= GameHandler.gameXml.movement.@ySpeed;
maxSpeedX=GameHandler.gameXml.movement.@maxSpeedX;
maxSpeedy=GameHandler.gameXml.movement.@maxSpeedY;
xDecel = GameHandler.gameXml.movement.@maxSpeedY;
yDecel = GameHandler.gameXml.movement.@xDecel;
jumpHeight= GameHandler.gameXml.jump.@height;
jumpRepeat= GameHandler.gameXml.jump.@repeat;
gravity = GameHandler.gameXml.level.@gravity;
fall = GameHandler.gameXml.jump.@fall;


}




}

}

and here is the sprite it is trying to control:
Code: [Select]
package Tristan
{
import org.flixel.*;
import org.flixel.plugin.photonstorm.*;
import org.flixel.plugin.photonstorm.FlxControl;;

/**
* ...
* @author Tristan Angeles
*/
public class Shifter extends FlxSprite
{
[Embed(source = "../Assets/player1img.png")] private const image:Class;
[Embed(source = "../Assets/player2img.png")] private const image2:Class;
[Embed(source = "../Assets/playerImg.png")] private const test:Class;

private var _currentPlayer:Player = null;
private var _id:String;

public function Shifter(X:Number,Y:Number):void
{
super(X, Y);

immovable = false;
this.immovable = false;

//delete afterwards
//testCode();

}

public function initGraphics():void
{
//trace(_currentPlayer.PlayerID);
if (!_currentPlayer) throw new Error("_currentPlayer not set");


var spriteImage:Class;
switch(_currentPlayer.PlayerID)
{
case "player1":
trace("setting "+_currentPlayer.PlayerID+" image to "+"player1img");
spriteImage = image;
_id = "Max";
break;
case "player2":
trace("setting "+_currentPlayer.PlayerID+" image to "+"player2img");
spriteImage = image2;
_id = "Sam";
break;
default:
trace("setting "+_currentPlayer.PlayerID+" image to "+"default");
spriteImage = image;
break;

}



loadGraphic(spriteImage, true, true, 32, 64);
//scale = new FlxPoint(2, 2);
//add animations
this.addAnimation("move", [0, 1, 2, 3], 10, true);
this.addAnimation("idle", [4, 5, 6, 7], 5, true);

}


public function get Pos():FlxPoint
{
var pos:FlxPoint = new FlxPoint();
pos.x = this.x;
pos.y = this.y;

return pos;
}

public function set currentPlayer(player:Player):void
{

_currentPlayer = player;
trace("current player has been set to" + player.PlayerID);


}

public function get currentPlayer():Player
{
return _currentPlayer;
}


override public function update():void
{
super.update();



trace(velocity.x);
if (velocity.x != 0)
{
play("move");
trace("moving");
}

else
{
play("idle");
}


super.update();

}


public function Show():void
{
if (!_currentPlayer) return;
trace(_id + " current controller is" + _currentPlayer.PlayerID);
}


public function testCode():void
{
if (FlxG.getPlugin(FlxControl) == null)
{
FlxG.addPlugin(new FlxControl)
}

FlxControl.create(this, FlxControlHandler.MOVEMENT_ACCELERATES, FlxControlHandler.STOPPING_DECELERATES, 3, true, false);
FlxControl.player3.setCursorControl(false, false, true, true);
FlxControl.player3.setMovementSpeed(400, 0, 200, 200, 400, 0);
FlxControl.player3.setJumpButton("UP", FlxControlHandler.KEYMODE_PRESSED, 250, FlxObject.FLOOR, 250, 200);
FlxControl.player3.setGravity(0, 400);

}



}

}



Thanks!
Check out my game journal: http://alternatdev.wordpress.com