some trouble with FlxState->FlxSprite.x read/write

  • something work wrong , i can not read and write Sprite position and other variables look at code please(the error should be inside Player.hx move function. can not read this.x , this.x0 parameters ):


    import flixel.FlxSprite;
    import flixel.system.FlxAssets.FlxGraphicAsset;
    import Math;
    import flixel.FlxG;
    import flixel.util.FlxTimer;
     * ...
     * @author user
    class Player extends FlxSprite
    	public var actiontimer:FlxTimer;
    	public var lastUpdate:Float;
    	public var speed:Int = 2000;//player move speed mm(px) per sec
    	public var dt:Float;//time elapsed ms from move start moment
    	public var x0:Float;//start move point coo x
    	public var y0:Float;//start move point coo y
    	public var x1:Float;//stop move point coo x
    	public var y1:Float;//stop move point coo y
    	public var dx:Float;//x1-x0
    	public var dy:Float;//y1-y0
    	public var movetime:Float;//were recount with use sin 30 degrees per y axis before start new move action
    	public function move(dt:Float, ?x1:Float, ?y1:Float):Void
    		//change [x,y] = f(dt) position between x0,y0 and x1,y1 of horisontal player sprite displacement
    		if (dt > 0){
    			//continue move
    			this.dt = dt;
    			this.x = this.x0 + this.dx * this.dt / this.movetime;
    			this.y = this.y0 + this.dy * this.dt / this.movetime;
    			//abort not completion move and/or start new move
    			this.x0 = this.x;
    			this.y0 = this.y;
    			this.x1 = x1;
    			this.y1 = y1;
    			this.dx = this.x1 - this.x0;
    			this.dy = this.y1 - this.y0;
    			//dimensions different
    			this.movetime = Math.sqrt(this.dx * this.dx + this.dy * this.dy / 4) / this.speed;//[sec] dy*dy/4 = (dy/sin30)*(dy/sin30)
    	public function new(?X:Float=0, ?Y:Float=0, ?SimpleGraphic:FlxGraphicAsset) 
    		actiontimer = new FlxTimer().start(20);
    		super(X, Y, SimpleGraphic);


    import flixel.FlxG;
    import flixel.FlxSprite;
    import flixel.FlxState;
    import flixel.text.FlxText;
    import flixel.ui.FlxButton;
    import flixel.math.FlxMath;
    class PlayState extends FlxState
    	var _court:FlxSprite;
    	var _player0:Player;
    	var _player1:Player;
    	public var pselapsed:Float;
    	override public function create():Void
    		_court = new FlxSprite(0, 0, "assets/images/court/court2.png");
    		_player0 = new Player(0, 0, "assets/images/players/15.png");
    		_player1 = new Player(500, 500, MenuState.ballcolor);
    		add(_player1);, "elapsedTime", "");,"lastUpdate","p.lUpd");, "dt", "p.dt");,"x0", "p.x0");,"x", "p.x");,"x1", "p.x1");,"movetime", "");
    	override public function update(elapsed:Float):Void
    		pselapsed += elapsed;"pse", pselapsed);
    		_player0.lastUpdate += elapsed*2;
    		_player1.lastUpdate += elapsed;
    		if (FlxG.mouse.pressed){"upd", elapsed);
    			if (FlxG.mouse.screenX > 3668 / 2){
    				_player0.x = 3000;
    				_player0.x = 1000;

    this is screen video show results

  • Not sure what you're trying to do but if you want to move the player, you should take a look at FlxObject::velocity. It's probably better than what you're doing.

    For example:

    player.velocity.set(50, 0); // will move there player to the right 50 pixels per second

  • @DleanJeans i can't do comfort, because in the future i will need calculate ball flying trajectory project (use sin30deg per y axis, that simulate vertical displacement per z-axis)
    i try just offset sprite position use player.x player.x0(start position) , player.x1(end position) , and dt - time of action
    the problem is in i can not read player.x and player.x0 inside body of class. x1 transport into function from outside and this correct behavior. but p.x and p.x0 have as class instance parameter. why i can not read/write this.x this.x0. it look strange

  • So you want to move the players to mouse position, you should use FlxVelocity::moveTowardsMouse(). It's better than reinventing the wheel.

    If you want to do some calculations, the velocity of the player is a FlxPoint which stores x, y values at velocity.x and velocity.y.

  • @DleanJeans and if i want move sprite follow trajectory calculated in function and with not constant speed? as i see standart methods allow move sprites comfort only with constant speed. not with acceleration

  • Scroll up to the top and there's accelerateTowardsMouse for you

  • @DleanJeans this step solved. was error if statement logic in PlayState.hx. and it born start error NaN, which transport to each next step.
    now it look terrible, but not dead 8)
    this is short video

Log in to reply