Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flash and Javascript

Status
Not open for further replies.

jray2003

Programmer
Aug 12, 2003
253
US
I need to do a Flash screen that does thing following

1. Display randomly a button that is either Red or Green when the program is loaded.
2. When the button is display it starts a timer.
3. The time stops when the button is clicked on and displays the results.
4. Also, keep track how many times the green or red button was pressed.

I know Javascript will be used, but I am not sure how to start so if there is a resource this would be truly helpful or even better, an example how this can be accomplished.

Thank you

Jim
 
Hi Jim,

You don't need javascript at all. There is nothing there that you can't do within Flash using Actionscript.

Create your two buttons in Flash and place them on the stage of the main timeline.

Set the buttons instance names to red and green.

Add two frames to your timline so that you have 3 frames total.

Add a layer and name it "actions".

On the first frame of the "actions" layer add the following code:

Code:
//define buttons as _global variables
_global.button0 = _root.green;
_global.button1 = _root.red;

//set inital values for counts
if(_global.greenCount == null && _global.redCount == null){
	_global.greenCount = 0;
	_global.redCount = 0;
}

//randomize function
_global.randomizeButton = function(){
	_global.button0._visible = false;
	_global.button1._visible = false;
	
	diceRoll = random(2);
	
	if(diceRoll == 0){
		_global.button0._visible = true;
	}else if(diceRoll == 1){
		_global.button1._visible = true;
	}
	_global.myTimer = getTimer();
	gotoAndPlay(2);
	
}

//function called by buttons
_global.stopTimer = function(){
	resultTime = Math.round((getTimer()-_global.myTimer)*.001);
	_root.greenCount.text = _global.greenCount;
	_root.redCount.text = _global.redCount;
	_root.gotoAndStop(1);
	trace(resultTime+" seconds");

}

_global.randomizeButton();

"Layer1" should have your buttons on it. Insert two text fields (frame 1 layer 1) with the instance names of "redCount" and "greenCount".

On the frame 3 "actions" layer:
Code:
gotoAndPlay(2);

Then on the buttons:

Code:
on(release){
	_global.greenCount = _global.greenCount+1;
	_global.stopTimer();
}
(change to redCount for the red button)

Then test your movie! :)

Hope it helps.


Wow JT that almost looked like you knew what you were doing!
 
Can I ask you a dumb question. I understand most of this except about adding the two text fields. Where do I find this option?

Thanks again..

Jim
 
Use the text tool. :) Then in the properties window change the property of the text box to dynamic.

Hope that's what you needed!

Wow JT that almost looked like you knew what you were doing!
 
Pixl8r

I think I am getting close but could you please take a look at this. I am learning a lot with this, but the gray matter is getting in the way. I know once I understand this, the rest will be easy. I am using Flash MX 2004 and some of the references on tutorial sites don't match and this is slowing me down. Here is the link for the file and you can see what I have done so far.

http:\\itheplace.com\drop\Reaction_Time.fla

Thanks again and thanks for not making me feel stupid....

Jim
 
On Layer1 remove the keyframe on frame 2 and frame 3. Otherwise you have it right.

Wow JT that almost looked like you knew what you were doing!
 
OK, I took out the two frames ran it, but didn't see anything new. What is it suppose to look like when I run it?

Thanks again for you time and this is fun to learn.

http:\\itheplace.com\drop\Reaction_Time.fla


Jim
 
BINGO!!!!

I think I got it. Now I need to spif it up and add a start start button and I think that is it.

Here is what I plan on doing. Insert a new frame 1 in the action and Layer 1 and add another button that says START and then it should call you code for the rest.

Thanks again for the help.

Jim
 
Can I ask one more question. I want to do the following.

Frame1: Have a Start or Reset button. They reset button will reset the counter.

Frame2-Action Layer: Would have the action script for the random buttons.

Frame3: Nothing
Frame4-Action Layer: Stop and display results. Another button will have a restart button.

Now, can I do this with the code what I have and just add the proper code for it?

Thanks again.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top