Spawn / Teleport Problem


(I'll tell you what I want, what I really really want) #1

Hello again, yes, it’s me, and I have another problem.

See, I’m trying to make my player spawn in x:200, y:200, every time he hits an entity OR he’s y is bigger than the screen’s.

Here’s my code:

	if (collide("KoloJedenCzerw", x, y + 1))
	{
		sprPlayer.play("hidePLAYER");
		sprPlayer.x = 200;
		sprPlayer.y = 200;
		sprPlayer.play("showPLAYER");
	}
	
	if (sprPlayer.y >= 600)
	{
		FP.world.remove(Player(x));
		FP.world.add(Player(200, 200));
		sprPlayer.play("showPLAYER");
		sprPlayer.play("stillPLAYER");
	}

And now, you can see that I’ve tried two options here, removing, and changing player’s position by modifying x and y. And nothing is happening. He just jumps and falls in to nothing, disappearing off the screen.

Any advice?


(Justin Wolf) #2

I think your problem may be because you’re using your sprite’s position instead of the Entity's position. Your sprite’s position is always relative to where the container Entity is located. So always use the Entity's x and y properties if you want actual location in the World. You can also condense all of that code into one single line using an OR operator (||). Try this instead:

if (collide("KoloJedenCzerw", x, y+1) || y >= 600)  x = 200, y = 200;

(I'll tell you what I want, what I really really want) #3

That works with falling down the y hole, thanks man!

But still, he hits the entity and just falls, instead of immediately spawning.

AND, I don’t want to start another thread, but is there an efficient way to play two animations (sprPlayer.play(“whatever”, if you know what I mean), one after another? Because when I write the second’s one code it seems like it skips the first one.


(Jacob Albano) #4

Spritemaps can only play one animation at a time. Are you looking for a way to queue them up?


(I'll tell you what I want, what I really really want) #5

Yes I am, @jacobalbano

First, after my player dies I spawn him with an animation of him appearing (opacity) and then I play the “static” position of him.

But it only seems to play the second one.


(Jacob Albano) #6

There’s no built-in way to do that, but you can get around it like this:

// in your constructor or whatever
sprPlayer.callback = onAnimationFinished;
queue = [];

// a property on your class
private var queue:Array;

// functions on your class
private function onAnimationFinished():void
{
    if (queue.length > 0)
    {
        var next:String = queue.pop();
        sprPlayer.play(next);
    }
}

private function queueAnimation(name:String):void
{
    queue.unshift(name);
}

(I'll tell you what I want, what I really really want) #7

Now this is what I’ve tried to do, unsuccessfully:

First I create a var

private var _counter:Number = 0; 

then:

	if (collide("KoloJedenCzerw", x, y + 1) || y >= 600)
	{
		x = 200;
		y = 200;
		_counter += 1 * FP.elapsed; 
		sprPlayer.play("showPLAYER");
		if (_counter == 2)
		{
			sprPlayer.play("stillPLAYER");
			_counter == 0;
		}
	}

Now, why this failed?

I would try your way @jacobalbano but at first sight it seems a bit complicated and I wanted to find my own solution, so I don’t have to ask about explaining.


(Jacob Albano) #8

Your way will probably only work once in about a thousand tries. You’re checking _counter == 2, but since _counter is a Number it will almost never be exactly equal to 2. Use _counter >= 2 if you want to do it your way.

As for my solution, it’s not complicated at all. Here’s the way it works:

  • When we want to enqueue an animation, we add it to the front of an array.
  • When any animation finishes, we check to see if the array has any contents.
  • If it does, we remove an element from the end of the array.
  • We then use that element (an animation name) to play the appropriate animation.

Since we’re adding to the front (with unshift()) and removing from the back (with pop()), we’ve created a rudimentary FIFO queue.


(I'll tell you what I want, what I really really want) #9

Arite then, it’s sorted! Thanks again!

One question tho, again, because I don’t want to create another thread:

I need to access some entity’s x and y in the other entity class. So,

private var _someEnt:someEntity

		if (something)
		{
			x = _someEntity.x;
			y = _someEntity.y; 
		}

And a question, why is this not working?


(Jacob Albano) #10

Besides the fact that all of those variables have different names?


(I'll tell you what I want, what I really really want) #11

that’s just an example.

This is the actual one:

private var _portalDOWN:PortalDOWN;

		if (collide("portalUP", x, y + 1))
		{
			x = _portalDOWN.x;
			y = _portalDOWN.y;

		}