Help with Instance variables and on-touch

  • 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!

  • 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.

    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.

  • 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:

    <img src="http://i1310.photobucket.com/albums/s651/E2GameStudios/ontouched_zps244541c5.jpg" border="0" />

    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?

  • 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?

    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! :)

  • 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?

  • 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?

    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 :)

  • 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:

    <img src="http://i1310.photobucket.com/albums/s651/E2GameStudios/ontouched2_zps3da472ed.jpg" border="0" />

  • By the Way: I lose and I get no awesome Balls! Fun game Man.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • By the Way: I lose and I get no awesome Balls! Fun game Man.

    Thanks :)

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

    Touch demo

  • 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.

  • There is an important distinction between your project example and my own that may be of note. In your example you have your sprite object with 5 animations and each animation has 1 frame. In my project My LvlSel Sprite object has multiple animations as well...but each animation has 11 frames. In my set up when the player touches the designated object (the "Foreward" sprite object) Animation 1 runs through all 11 frames...then on the next touch it runs through all 11 frames of the 2nd animation...and so on. What I need now is for a player to be able to touch the "Reverse" object and have the animation (which ever animation of the LevSel object is CURRENTLY on) run in reverse. Then if the "Reverse" object is touched again run the proceeding proceedinganimation in reverse. Inversely if the player touches the "forward" object the LvlSel sprite runs the animation in forward order from where ever it left of last.

    I know I need some kind of code logic that 1st checks to see what the current animation just finished playing. Then 2nd, depending on what animation of the levSel sprite object it is on it runs another animation of that sprite object in either forward or reverse depending upon which object is touched (The "Forward" sprite object or the "reverse")

  • I made a quick demo that represents what I am trying to do.

    I left my latest attempt to run the animations in reverse in place but toggled disabled and I set it up as an on click rather than a on touch in case you don't have a touch screen device. I know you can easily replace that condition but you are helping me so I figured I could make it easy.

    dropbox.com/s/akuq9oc9lijhukc/Touch_Click_Animation.capx

  • Here are few posts about reversing animations:

    scirra.com/forum/play-animation-in-reverse_topic48894.html

    scirra.com/forum/need-to-reverse-animation_topic55826.html

    There are various solutions for the problem. Here is one implementation, hopefully it's close what you were looking for: Animated level select

    Mouse clicks 'simulate' touch events by the way, so you can use touch object even if you use a mouse. :)

    If you wish to include me in the credits, I have no objections, would be an honor :)

  • Your example is fantastic! It does far more to help me understand the specific programming nature of my issue than the posts but the posts help me understand the terminology so thank you on both fronts.

    I still have a long way to go to understand the expressions because I come from an Art background in my professional Game development experience rather than a programming. Things like "self.AnimationFrameCount-1" make sense when I see them but knowing how to create them still eludes me. My hope is that will come with time but seeing those expressions in a layout as it pertains to my specific needs are spectacular and I very much appreciate your efforts.

    On the mouse event...I realized that after I posted it but thanks for stating it as it may be of use to another reader at some later date.

  • Now if I may be so bold I would like to ask how I might prevent the animations at the beginning and end of the sequence from repeating. In our example file we have the steering wheel with "animation 3" (or animation3 in the sprite animations listing). If the player is at the end of the available animations I would like a click on the right arrow to do nothing (at this time it runs the animation again)...however I would like to preserve the functionality of going in reverse from that point. Inversely, if the player is on "animation1" a click on the left arrow should do nothing (presently it runs "animation1" in reverse) but the player is free of course to play the animation forward if they click the right arrow.

    Concerning the credits would you like just your Forum user name or your real name in them? If you would like your real name you are free to PM me on that. Your assistance on this has been invaluable and giving and to those who have helped me is the least I can do. If you ever feel like making some money on the side PM me on that too...my company s small but I do have a budget for contractors.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)