To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-09-2004, 02:59 PM   #1
erikbjork
Junior Member
 
Join Date: Oct 2004
Posts: 5
Random guessing game - actionscript error

Hi, im new to these forums. I'm making a guessing game - when you press one of three buttons, this script executes:

code:

on (release)
{
_root.win=0;
_root.win=random(2);

if (_root.win=0);
{ gotoAndStop(3); } //Lose

if (_root.win=1);
{ gotoAndStop(4); } //Win
}



The problem is that the player always wins. Where is the error?

Last edited by jbum; 10-09-2004 at 03:12 PM.
erikbjork is offline   Reply With Quote
Old 10-09-2004, 03:18 PM   #2
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,430
Hi,

couple of mistakes in your code, specially the needed double eq. sign in if statement:

code:
on (press) {
_root.win = 0;
_root.win = random(2);
}
on (release) {
if (_root .win==0 ) {
this.gotoAndStop(3);
} else if (_root .win==1) {
this.gotoAndStop(4);
}
}



gparis
__________________


AS 2.0 Forum Guidelines || Use P H P tags for code samples || Flash LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 10-09-2004, 03:18 PM   #3
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
The problem is your use of a single equals sign in the if statement. There are two similar looking operators

= (assignment)
== (equality)

When you are testing for equality, such as in an if statement, you need to use the second one (which is two equal signs). Otherwise, it assumes you want to assign the value on the right side to the variable on the left side.

Also, you should omit the semicolons after the if statement, they will cause it to execute incorrectly.

It's

if (cond) statement; or
if (cond) { block; }

not

if (cond); statement; and not
if (cond); { block }

Here's are some rewrites of your code which simplifies some other things.

code:

on (release)
{
_root.win=0;
_root.win=random(2);

if (_root.win==0)
{
gotoAndStop(3);
} //Lose
if (_root.win==1)
{
gotoAndStop(4);
} //Win
}




Simplification #1. Use the 'else' clause.

code:

on (release)
{
_root.win=0;
_root.win=random(2);

if (_root.win==0)
{
gotoAndStop(3);
} //Lose
else
{
gotoAndStop(4);
} //Win
}



Simplification #2. Omit the variable.

code:

on (release)
{
if (random(2)==0)
{
gotoAndStop(3);
} //Lose
else
{
gotoAndStop(4);
} //Win
}




Simplification #3. Use the random value to compute the frame number. This is clever, but not particularly easy to understand - I would probably use the previous method.

code:

on (release)
{
gotoAndStop(3 + random(2));
}



- Jim
__________________
jbum is offline   Reply With Quote
Old 10-09-2004, 03:41 PM   #4
erikbjork
Junior Member
 
Join Date: Oct 2004
Posts: 5
Thank you so much!
You guys really helped a newbie out!
erikbjork is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:02 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.