Hello Guest

Author Topic: delegate  (Read 10822 times)

mike2d

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 63
    • View Profile
delegate
« on: June 22, 2012, 08:43:38 am »
hi,

very new to 2d toolkit and 2d animation in general :)

i'm experiencing some strange behavior with my sprite animations.

it's a simple sprite (a character) who walks right and left. but when i stop pressing "A" or "D" button, the animation stops in a strange position and the object is offset upward...

I saw there was an "animation complete delegate" function. actually, i don't understand it's purpose and wish someone could explain it to me and maybe give some tutorial examples (i've seen the example given by 2D Toolkit but isn't enough to understand).

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: delegate
« Reply #1 on: June 22, 2012, 10:18:30 am »
The animation complete delegate is called when the animation has completed playing, i.e. after the last frame has been played.

mike2d

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 63
    • View Profile
Re: delegate
« Reply #2 on: June 25, 2012, 10:19:30 am »
ok so what's the solution to stop an animation in the middle because i released a key and set the character back to it's idle position or to keep it playing when the animation cycle is finished because i'm still pressing the key ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: delegate
« Reply #3 on: June 25, 2012, 10:25:41 am »
Somehow that post got submitted before I finished it.

Ok. Set your clip to looped, and then press play if its not already playing when your press a direction key. When the key is let go, call StopAndResetFrame or Play("idle") or something like that to switch to idle.


So something like this -

if (Input.GetKey(KeyCode.A))
{
  if (animatedSprite.IsPlaying() && animatedSprite.CurrentClip.name != "walkleft")
      animatedSprite.Play("walkleft");
}
else
{
  if (animatedSprite.IsPlaying() && animatedSprite.CurrentClip.name == "walkleft")
     animatedSprite.Play("idle");
}

Treat that as pseudocode, it hasn't been tested, but conveys the main concept.
« Last Edit: June 25, 2012, 11:15:06 am by unikron »

mike2d

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 63
    • View Profile
Re: delegate
« Reply #4 on: June 25, 2012, 10:47:55 am »
wow,

so you have 2 objects "animation" & "animatedSprite" ? how do you declare them ?

by the way, there is my code with the added line from yours (
Code: [Select]
if (animation.IsPlaying()...) but no animation fires... :

Code: [Select]
void Start ()
{
anim = GetComponent<tk2dAnimatedSprite> ();
}

void Update ()
{

if (Input.GetKeyDown ("space")) { // jump
rigidbody.AddRelativeForce (transform.up * jumpSpeed, ForceMode.Impulse);
}

// if not hiding
if (!hiding) {

movement = Input.GetAxis ("Horizontal") * moveSpeed; 
movement *= Time.deltaTime; 
transform.Translate (movement, 0.0f, 0.0f);

if (Input.GetKeyDown (KeyCode.S)) { // hide
anim.Play ("hide");
hiding = true;

} else if (Input.GetKeyDown (KeyCode.D)) { // walk right
if (anim.isPlaying () && anim.CurrentClip.name != "walkleft")
anim.Play ("walk");
if (!goingRight)
anim.FlipX ();
goingRight = true;

} else if (Input.GetKeyDown (KeyCode.A)) { // walk left
if (anim.isPlaying () && anim.CurrentClip.name == "walkleft")
anim.Play ("walk");
if (goingRight)
anim.FlipX ();
goingRight = false;

} else {
anim.Play ("idle");
}

} else {

// unhide
if (Input.GetKeyDown (KeyCode.W)) {
anim.Play ("unhide");
hiding = false;
}
}
}
« Last Edit: June 25, 2012, 10:58:30 am by mike2d »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: delegate
« Reply #5 on: June 25, 2012, 11:08:57 am »
Sorry, that was a typo - they are both the same object. I've changed both of them now.

Currentclip will never be "walkleft" will it, as you never Play a clip with that name. I suspect this will just get stuck on the first frame, and when you let go of the key, it will play a full sequence. Is that right?


mike2d

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 63
    • View Profile
Re: delegate
« Reply #6 on: June 25, 2012, 11:14:44 am »
no problem, but you still have 2 objects in the code :

  if (animatedSprite.IsPlaying() && animatedSprite.CurrentClip.name != "walkleft")
      animation.Play("walkleft");
}

is this right ? or it's always the same object ?

about my code, no animation at all is playing. actually, it seems only the first frame of "idle" animation is drawn then nothing.... ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: delegate
« Reply #7 on: June 25, 2012, 11:16:48 am »
Missed that - they are all the same object. Should be fixed now unless I've really gone blind!

With your code, can you check that the anim.Play() function is actually called? Set a breakpoint or add a Debug.Log in there, as if hiding is true, the whole code block wont execute...

mike2d

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 63
    • View Profile
Re: delegate
« Reply #8 on: June 25, 2012, 12:10:26 pm »
it is called, a debug.log is printed when D is pressed.

mike2d

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 63
    • View Profile
Re: delegate
« Reply #9 on: June 25, 2012, 02:47:19 pm »
finally,

i figured out the problem.

first of all, you were right. it is necessary to check if an animation is playing and which one, otherwise at each update it starts again (no animation visible then).

then, by using "A" and "D" as move keys, unity returns true only for the CURRENT FRAME even if the key stays pressed... instead i used horizontal values returned by getAxis.

here's my working code for information :

Code: [Select]
void Update ()
{
if (Input.GetKeyDown ("space")) { // jump
rigidbody.AddRelativeForce (transform.up * jumpSpeed, ForceMode.Impulse);
}

// if not hiding
if (!hiding) {

movement = Input.GetAxis ("Horizontal") * moveSpeed; 
movement *= Time.deltaTime; 
transform.Translate (movement, 0.0f, 0.0f);

if (Input.GetKeyDown (KeyCode.S)) { // hide
if (anim.isPlaying () && anim.CurrentClip.name != "hide" || !anim.isPlaying ())
anim.Play ("hide");
hiding = true;

} else if (Input.GetAxis ("Horizontal") > 0) { // walk right
if (anim.isPlaying () && anim.CurrentClip.name != "walk" || !anim.isPlaying ())
anim.Play ("walk");
if (!goingRight)
anim.FlipX ();
goingRight = true;

} else if (Input.GetAxis ("Horizontal") < 0) { // walk left
if (anim.isPlaying () && anim.CurrentClip.name != "walk" || !anim.isPlaying ())
anim.Play ("walk");
if (goingRight)
anim.FlipX ();
goingRight = false;

} else {
if (anim.isPlaying () && anim.CurrentClip.name != "idle" && anim.CurrentClip.name != "unhide" || !anim.isPlaying ()) {
anim.Play ("idle");
}
}

} else {

// unhide
if (Input.GetKeyDown (KeyCode.W)) {
if (anim.isPlaying () && anim.CurrentClip.name != "unhide" || !anim.isPlaying ()) {
anim.Play ("unhide");
}
hiding = false;
}
}
}

thank you for your help, i appreciated your responsiveness !

mike
« Last Edit: June 25, 2012, 02:51:45 pm by mike2d »