Making entity get picked up by another entity [SOLVED]


(John Andersson) #1

Hi. I have a sword with this code in the update function:

		if (collide("hero", x, y))
		{
			if (!pickedUp)
			{
				pickedUp = true;
			}
		}

		if (pickedUp)
		{
			x = "hero".x
			y = "hero".y //fixed typo :p
		}

It doesn’t work!

Here is the hero class:

	public function Hero(p:Point) 
	{
		blabalbal
		
		type = "hero";
            }

Essentially, what I want to do is to make the sword follow the ENTITY that touches it. So if an enemy touches it, it follows that enemy, a.k.a the enemy PICKS IT UP.

Thank you in advance :slight_smile:


(Jacob Albano) #2
x = "hero".x
y = "hero.y"

It’s obvious to me what you’re trying to do here, but these two lines are both wrong in different ways. :stuck_out_tongue:

In the first line, you’re accessing the x property of a string, but there’s no such thing. That should cause a compile error.

In the second line, you’re setting a variable of type Number to a string value. Depending on your compiler settings, that might actually be allowed, but it won’t give you the result you’re looking for.

What you want to do is save the result of your collide() call, then use it later:

var e:Hero = collide("hero", x, y);
if (e)
{
    if (!pickedUp) pickedUp = true;
|

if (pickedUp)
{
    x = e.x;
    y = e.y;
}

(Jacob Albano) #3

If you want your weapons to be usable by either the Hero or the Enemy, you can do something like this:

  • Create a base class Character that both Hero and Enemy extend.
public class Character extends Entity
{
    public var weapon:Weapon;

    public function Character()
    {
        type = "character";
    }

    override public function update()
    {
        super.update();
        if (weapon != null)
        {
            weapon.x = x;
            weapon.y = y;
        }
    }
}
  • When you check for collision in your weapon, do type checking to determine any special behavior, if necessary.
var e:Hero = collide("hero", x, y);
if (e)
{
    if (e is Hero)
    {
        // do some hero stuff
    }
    else if (e is Enemy)
    {
        // or some enemy stuff
    }

    if (!pickedUp) pickedUp = true;
}

(John Andersson) #4

Thank you SO MUCH! You’re the best!! Sorry for messaging you :stuck_out_tongue: I totally don’t regret it now, though! :smiley: But I won’t message you again, since it’s a bit annoying, I guess. I’ll simply wait the next time. THANK YOU SO MUCH! Feeling euphoric now.

EDIT: I’m gonna go with hero-pickups only for now, and add the enemy pickup option later. I ran into a small error for now, though!

	override public function update():void 
	{
		var e:Hero = collide("hero", x, y);
		
		//Make hero pick it up
		if (e)
		{
			if (!pickedUp)
			{
				pickedUp = true;
			}
		}

blablabalbal }

“col: 17 Error: Implicit coercion of a value with static type net.flashpunk:Entity to a possibly unrelated type Hero.”


(Jacob Albano) #5

My bad, sorry. Try this:

var e:Hero = collide("hero", x, y) as Hero;

(John Andersson) #6

Awesome! A problem though, it seems as if the weapon falls behind a bit. It’s a bit slower than the hero. Is there any way I can “snap” it to the hero?

Another problem! If I change the code to

                    if (pickedUp)
		{
			x = e.x + 105;
			y = e.y;
		}

to make it look like the hero holds the weapon in his hand.

If I go backwards or jump, the hero just freezes! And if I increase the number to 200, the hero freezes instantly when he picks up the weapon. Weird!


(Jacob Albano) #7

You’re probably seeing the one-frame lag caused by the weapon’s position being updated to the hero’s position one frame after he moves. If I recall correctly you can mitigate this by setting its position in an override of the render() function.


(John Andersson) #8

Hmm. Is there any specific code you could link so I know the commands? And do you know what is causing the freezing??


(Romko Pidstryhach) #9

If this code is in your hero class, then you’re setting his position to weapon position.

if (pickedUp)
	{
		x = e.x + 105;
		y = e.y;
	}