Change the actor’s image with arrows keys

Updated on August 8, 2014 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on August 8, 2014

Hi,
I’m trying to develop a game like [Parappa the rapper] but I’m a little bit lost with this.
I already know how to listen the keyboard events, but I don’t know how to change the image when the user tap an arrow key.

This is my javascript code:


var moveUp : KeyCode;

var moveDown : KeyCode;
function Update () {

if (Input.GetKey(moveUp))

{

//Change image from sprite

}

else if (Input.GetKey(moveDown))

{

//Change image from sprite

}

else

{

//Change image from sprite

}

}

Can anybody help me?
Thanks 🙂

  • Liked by
Reply
0 on August 8, 2014

you could have several different gameobjects as children, each with their own sprite, and just disable them accordingly.
aka:

var GameObject upSprite;
var GameObject downSprite
if(Input.GetKey(moveDown))
{
    downSprite.SetActive(true);
    upSprite.SetActive(false);
}
  • Liked by
Reply
Cancel
1 on August 8, 2014

Hi Panda,You need to modify the sprite renderer of the game object. 🙂 

var moveUp : KeyCode;
var moveDown : KeyCode;
//Sprites
var moveUpSprite : Sprite;
var moveDownSprite : Sprite;
var moveElseSprite : Sprite;
function Update () {
if (Input.GetKey(moveUp))
{
//Change image from sprite
 GetComponent(SpriteRenderer).sprite = moveUpSprite;
}
else if (Input.GetKey(moveDown))
{
//Change image from sprite
 GetComponent(SpriteRenderer).sprite = moveDownSprite;
}
else
{
//Change image from sprite
 GetComponent(SpriteRenderer).sprite = moveElseSprite;
}
}

Hope this helps you with the game. ^^ 🙂

on August 8, 2014

It works perfect! Thanks 🙂

Show more replies
  • Liked by
Reply
Cancel