Help with Instance variables and on-touch

Get help using Construct 2

Post » Wed Apr 03, 2013 9:47 am

Ok everyone...I can find plenty of information for instance variables for objects like sprites but I finding nothing on setting an instance variable for something like a touch command.

Here is what I am trying to do: I have a sprite object with multiple animations and I need to figure out how to have an On Touched event play one of the various animations depending on the number of touches on the associated object.

This is probably very simple and I have just gotten so wrapped up in it that I cant see the obvious solution but at this point I could really use a hand. Honestly I am not entirely sure I even need an instance variable to do this but I have yet to find an alternative so I am open to any suggestions that will ultimately help me get the animations associated with the on touched object (or on click for that matter)to run in accordance with the number of touch/clicks.

Thanks in advance!
B
11
S
2
Posts: 65
Reputation: 1,396

Post » Wed Apr 03, 2013 9:57 am

[QUOTE=E2GameStudios]

Here is what I am trying to do: I have a sprite object with multiple animations and I need to figure out how to have an On Touched event play one of the various animations depending on the number of touches on the associated object.
[/QUOTE]

So you wish to play certain animation based on how many times the object has been touched?

You could do something like this:

sprite instance variable: touches = 0

on touched sprite
.. add 1 to sprite.touches
.. set sprite animation to "animation" & sprite.touches

I assume that you have named animations of sprite as animation1, animation2, animation3 and so on.
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Thu Apr 04, 2013 6:33 am

I am a newbie here so bear with me please.

I think I understand the 1st part of your instructions: You are saying to create an instance variable within the properties of the sprite named "touches"...with an initial value set to 0.

But I am a bit confused with the 2nd part: I have created an "On Touched (sprite name)" event. I go to Add action/sprite (the one with the animations and the instance variable from step 1)/add to...I select the only option in the drop down called "touches"...set the value 1...click done.

Then I click add action again...select the sprite (with the animations and new instance variable associated with it)...choose set animation...type the name of my 1st animation between the quotation marks and then add &Spritetouches behind that with no spaces. I leave beginning selected in the FROM dropdown and hit done.

I end up with this:



In the example my sprite object name is "LevSel" and the animations associated with that sprite are "Act1toCh1", "Ch1toCh2", etc. I have the Stop Animation set up to prevent the 1st animation from running before any touches happen.

Unfortunately this does not appear to work. What am I missing?
B
11
S
2
Posts: 65
Reputation: 1,396

Post » Thu Apr 04, 2013 6:45 am

[QUOTE=E2GameStudios]
In the example my sprite object name is "LevSel" and the animations associated with that sprite are "Act1toCh1", "Ch1toCh2", etc. I have the Stop Animation set up to prevent the 1st animation from running before any touches happen.

Unfortunately this does not appear to work. What am I missing?[/QUOTE]

Almost correct, except for the above part! :)

The idea is, that at your 'set animation' action you dynamically create the name of the animation you want to refer. So that would mean merging the touches variable as part of the created animation name.

What happens now, is that you try to set animation name as "Act1toCh1" & sprite.touches, which seeks for animation called "Act1toCh11" (notice the extra 1, which is value of touches variable).

What you want, is it to seek animation with touches variable replacing the last number. You can achieve it like this: "Act1toCh" & sprite.touches. So if your touches variable is 4, you would seek an animation called "Act1toCh4".

This technique is not the most obvious or clear to use, but it's something I personally use very often. Hope this clears it up! :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Thu Apr 04, 2013 8:32 am

So if i am understanding correctly I just append my sprite animations to allow for the touch count to find them...like "Act1toCh1_" (resulting in Act1toCh1_1) or Ch1toCh2_2?
B
11
S
2
Posts: 65
Reputation: 1,396

Post » Thu Apr 04, 2013 8:45 am

[QUOTE=E2GameStudios] So if i am understanding correctly I just append my sprite animations to allow for the touch count to find them...like "Act1toCh1_" (resulting in Act1toCh1_1) or Ch1toCh2_2?[/QUOTE]

Yup, that is the idea. You can just remove the last number in set animation action, so the action reads: set animation to "Act1ToCh" & Sprite.touches
No need to rename all animations unless you feel it is necessary :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Thu Apr 04, 2013 8:54 am

should it matter that the animations are named differently? and do I need to add separate actions for animation associated with the sprite object?

like so:

B
11
S
2
Posts: 65
Reputation: 1,396

Post » Thu Apr 04, 2013 9:20 am

By the Way: I lose and I get no awesome Balls! Fun game Man.
B
11
S
2
Posts: 65
Reputation: 1,396

Post » Thu Apr 04, 2013 9:54 am

[QUOTE=E2GameStudios] By the Way: I lose and I get no awesome Balls! Fun game Man.[/QUOTE]

Thanks :)

Here is a quick demo I made to show I might do this:

Touch demo
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Fri Apr 05, 2013 4:39 am

Thank you so much for the Capx. Once I got the latest beta version I was able to open it and compare your project to mine to see where I had gone wrong. I can get every animation to run in order with each click/touch. I even figured out how to use another sprite object (A "Forward" sprite) to be the source of the on touched event and still affect the animated sequences of the original sprite.

Now what I am trying to do is get the animations to run in reverse order if I click a 3rd sprite (called "Reverse"). I am not sure if I can somehow get the animations to run in reverse using the event sheet through some kind of event/action pairing...or if I need to set up "reverseAnimation1, 2, 3" in the original sprite's animation (and just re-import the frames and reverse them via right click in the Animations Frames window)...or just make a completely separate sprite object ("lvlSelRev" perhapse?) with reverse versions of the original animations and worry about creating or destroying sprites depending on if the player touches the "Forward" sprite object or the "Reverse". Do you have any suggestions? IF t all possible I would prefer to use the existing animations in the existing sprite object to but I would have no Idea how to set up my event sheet for that.

On a side note...I am making a Game I intend to sell in the windows 8 app store. I have a space in the End credits for people in the forums who help me out and unless you have any objections I would like to include your name or username there.
B
11
S
2
Posts: 65
Reputation: 1,396

Next

Return to How do I....?

Who is online

Users browsing this forum: tatogame and 27 guests