Player not showing?


(Matt Deon) #1

So my player spawns and moves perfect when its just the single image. BUT when I make it a sprite it spawns off to the side and falls right through the side blocks or doesn’t spawn at all. I really don’t want to share my source code so if you could help me without it that would be great or if I have to email me and I will send you the code. Oh and if helps my friend who makes the sprites actually made it so the sprites don’t evenly fit. I will have him fix it after but he is sleeping right now. If that could be causing the problem just let me know.

Thanks!


(Jacob Albano) #2

It’s helpful to both the forum and yourself if you can produce a minimal code example that demonstrates your problem. More often than not you’ll end up figuring it out on your own, too.

Can you narrow your code down to a few lines that demonstrates your problem?


(Matt Deon) #3

Well. I suppose it is only the player code I need to show so yes I can show you the code.

package {
import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
import net.flashpunk.FP;
import net.flashpunk.Sfx;
import net.flashpunk.graphics.Spritemap;

public class Player extends Entity 
{
    private var power:Number=0.2;
    private var jumpPower:Number=6;
    private var hFriction:Number=0.95;
    private var vFriction:Number=0.99;
    private var xSpeed:Number=0;
    private var ySpeed:Number=0;
    private var onTheGround:Boolean=false;
    private var gravity:Number=0.3;
	[Embed(source = "../assets/soldier.png")] private const PLAYER:Class;
	[Embed(source = "../assets/jump.mp3")] private const JUMP:Class;
	public var jump:Sfx = new Sfx(JUMP);
	public var soldier:Spritemap = new Spritemap(PLAYER, 27, 48);
    public function Player() 
	{
		soldier.add("left", [0, 1, 2, 3], 20, true);
		soldier.add("riht", [4, 5, 6, 7], 20, true);
        graphic=new Image(PLAYER);
        setHitbox(28,47);
        x=400;
        y=225;
    }
    override public function update():void {
        var pressed:Boolean=false;
        if (Input.check(Key.LEFT)) 
		{
            xSpeed-=power;
            pressed=true;
        }
        if (Input.check(Key.RIGHT)) 
		{
            xSpeed+=power;
            pressed = true;
        }

        if (collide("wall", x, y + 1))
		{
            onTheGround=true;
            ySpeed=0;
            if (Input.check(Key.UP))
			{
                ySpeed -= jumpPower;
				jump.play();
            }
        } else {
            ySpeed+=gravity;
        }
        if (Math.abs(xSpeed)<1&&! pressed) {
            xSpeed=0;
        }
        xSpeed*=hFriction;
        ySpeed*=vFriction;
        adjustXPosition();
        adjustYPosition();
    }
    private function adjustXPosition():void {
        for (var i:int=0; i<Math.abs(xSpeed); i++) {
            if (! collide("wall",x+FP.sign(xSpeed),y)) {
                x+=FP.sign(xSpeed);
            } else {
                xSpeed=0;
                break;
            }
        }
    }
    private function adjustYPosition():void {
        for (var i:int=0; i<Math.abs(ySpeed); i++) {
            if (! collide("wall",x,y+FP.sign(ySpeed))) {
                y+=FP.sign(ySpeed);
            } else {
                ySpeed=0;
                break;
            }
        }
    }
}

}

Anyways any help would be much appreciated! Thanks. :smiley:

EDIT: With the current code the whole sprite mad loads…So I have 8 of my player. lol


(Alex Larioza) #4

You assigned the graphic of your entity to a new image instead of the spritemap you created:

You’ll want to change it to:

graphic = soldier;

(Matt Deon) #5

I realized that after. Now the image shows up but plays no animation?


(Jacob Albano) #6

You have to manually play the animation; it won’t start it automatically. You can add this to your constructor to have the image play as soon as it is created:

soldier.play("left");

Also worth pointing out that you misspelled the animation name “right” as “riht”. That might cause you some trouble later on.


(Matt Deon) #7

Ya I realized that after thanks and I will try the fix you suggested when I get home.


(Matt Deon) #8

Thanks the animations finally work! :smiley: Now…The only thing is when loop is true it doesn’t stop and when false it only plays once. It won’t start again.

EDIT:Actually I added the true to the play statement and now it does reset but I figured it is because it only plays when you press the left arrow key to move. Not the whole time its being held. Any fix to this? lol


(Alex Larioza) #9

This is the intended behavior - setting “loop” to true plays endlessly while setting it to false only plays it once. If you only want the animation to play once, but want to play it again later, you’ll need to call the play() function again.

Can you post the relevant code?


(Matt Deon) #10
			if (xSpeed < 0) 
		{
			soldier.play("left", true);
		}
		if (xSpeed > 0)
		{
			soldier.play("right", true);
		}

This is added in the code I sent earlier.


(Alex Larioza) #11

Ah, sorry, I keep forgetting the code boxes scroll.

Here’s what I’d do to get an animation to play when the player is moving:

  • Set the loop option in the add() function to false
  • Within the if-statement where you are checking for key presses, add the play() function for the relevant animation. Don’t set the second parameter to true or it will keep resetting the animation.
  • Then add another check for when the player releases the movement keys and set the animation to play a standing animation.

Here’s an untested example:

anim.add("stand", [0], 0)
anim.add("left", [1,2,3], 10)
anim.add("right", [4,5,6], 10)

...

If (Input.Check(Key.RIGHT))
{
    anim.play("right")
}
else If (Input.Check(Key.LEFT))
{
    anim.play("left")
}
else
{
    anim.play("stand")
}

(Matt Deon) #12

That still doesn’t fix it. Here maybe this will explain better.


(Zachary Lewis) #13

It looks like your sizes are off. Are each of your sprites frames the same size?


(Matt Deon) #14

Well see that was in the main post. I accidentally made it wrong. I will fix it right now but I don’t think that would cause the animation to play wrong. Would it?


(Matt Deon) #15

Nevermind finally fixed it thanks to both of you! :smiley:


(Zachary Lewis) #16

What was the problem?


(Matt Deon) #17

Well I never had it set up to have a still animation so it would just play the one. So once setting a still frame and then deleting the loop true or false statement everything ran perfect. Thanks guys.