0
votes

I'm making a simple platformer game and want to know how to "pick up" a sprite and have it end the game. I can't find any resources anywhere and the only things I can find involve using the XNA platformer starter kit... something which I'm actively avoiding. So the basic idea is the player picks up an apple - if it's poisonous prompt the lose screen if it's made of gold prompt the win screen for example.

2

2 Answers

1
votes

I would assume your character has a collision box. Simply create a class of objects called Pickup and add these objects in your environment. Make these objects have a collision box too so the player can detect he's touching them, and add a OnPickup method on your objects to be called when the player picks them up (either by simply touching them or touch + input). Specialize your OnPickup for your various kind of pickups to produce the expected behavior (such as die or +x gold).

0
votes

You can check your player's Vector2 position against an item's Vector2 position, and see if their difference is less than, let's say 30 pixels:

if (Vector2.Distance(player.position, item.position) < 30)
{
    // player is close enough to an item to pick it up
}

You can also check against cursor position like that.