Game Development Community

Using setFrame with Static Sprites

by David Chutka · in Torque Game Builder · 07/06/2009 (5:47 pm) · 5 replies

Hi all,

Sorry again for a post that seems very simplistic to me. I got myself halfway to where I need to go, so hopefully someone here can help me get all the way there. I posted a week or two ago about using static sprites as buttons, but the shape i was using was causing too many issues, so I went back and simplified things. I have very little experience programming and feel stupid having to ask a question about something that most of you can probably solve in a second, but I'm going to ask anyway.

I have my buttons (static sprites) set up with three frames.
0 = white (default)
1 = green (active)
2 = red (ignore for now)

I have managed to set it up so that when you click on the buttons they advance to frame 1 and turn green. What i'm looking to do is have the buttons turn green when clicked once, then if you click on them again have them go back to frame 0. My best guess is to use some sort of "if" statement.

"if the button is set to frame 0, change to frame 1. if the button is set to frame 1, change to frame 0"

I have dug through the forums and tutorials and haven't successfully found a similar way to do such a thing. Listed below are all my lines of code that I managed to cobble together to magically get things to work this far.


function button::OnAdd(%this)
{
%this.setUseMouseEvents(true);

%this.setFrame( 0 );
}

function button::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
%this.setFrame( 1 );
}


As always, any help is always appreciated.

#1
07/06/2009 (6:26 pm)
Hey David, I tried out your code then added an if statement, and it worked nicely.

function Button::onLevelLoaded(%this, %scenegraph)
{
%this.setUseMouseEvents(true);

%this.setFrame( 0 );
}

function Button::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
   if(%this.Frame == 0)
       %this.setFrame( 1 );
   else if(%this.Frame == 1)
       %this.setFrame( 0 );
}

That code works exactly how your asking for it to work.
#2
07/07/2009 (6:41 am)
Absolutely fantastic. Now that I see it typed out it makes perfect sense. I feel like someone trying to live in another country while learning another language. I can read it without much trouble, but if you ask me to speak it i'm still slow or completely lost.

Eventually such questions won't need answering. But for now, I can't thank you enough.
#3
07/07/2009 (7:40 am)
No Problems!

Eventually this stuff will just come to you, all it takes is PRACTICE!
#4
09/15/2009 (12:42 pm)
HI there,
I hope you guys get this,
But this code, is just what I have been looking for, except, I want to be able to create a random frame and I am not sure how to do that. I have looked into it, and I am sure I would use the getRandom function but am unsure of how to plug it in and relate it to the setFrame call.


Any help would be great.
#5
09/15/2009 (1:27 pm)
Something like this:

%randomFrame = getRandom(0,%numFrames - 1)
%this.setFrame(%randomFrame);

where %numFrames is the total number of frames for your image.