all groups > flash actionscript > april 2007 >
You're in the

flash actionscript

group:

If/Else - Is there a cleaner code?


Re: If/Else - Is there a cleaner code? DMennenoh **AdobeCommunityExpert**
4/30/2007 5:35:42 PM
flash actionscript:
[quoted text, click to view]
if (_root.EarthClip._currentframe == 1) {
_root.EarthClip.gotoAndPlay(140);
} else { if (_root.EarthClip._currentframe == 160) {
_root.EarthClip.gotoAndPlay(156);


How about this?

on (rollOver) {
if (_root.EarthClip._currentframe == 1) {
_root.EarthClip.gotoAndPlay(140);
} else {
_root.EarthClip.gotoAndPlay(156 - (_root.earthClip._currentframe -
160));
}
}


--
Dave -
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/

If/Else - Is there a cleaner code? MShetler
4/30/2007 8:13:11 PM
Hello all,

I am currently working on a flash file and I'm an intermediate user. My
actionscript abilities are probably close to beginner, but I'm a quick learner
(most of what I have learned with actionscript has been self taught).

In my flash file I have an animation of a rotating globe. Each frame of the
rotation is a separate raster image - I don't know how you would represent a
rotating globe otherwise, not really something that can be tweened (suggestions
are welcome).

I have different locations off to the side of the globe. When the user mouses
over a location, the globe spins to that location. When they roll out, the
globe spins back to a static starting point.

I wanted the animation of the globe to be as smooth as possible, so, I have a
}else{ statement for every possible frame. IE - if the globe animation is
rotating back from a position and the user mouses over a new location before
the animation stops, it doesn't jump back to the starting static position, it
seemlessly flows from the position it is currently at to the new position the
user has moused over.

Make sense?

It works flawlessly as is. I've spent quite a bit of time testing it.
However, the code is bulky in appearance and according to the size report bulky
in bytes as well.

Is there a differnent way to set this up to cut down on size - mainly byte
size if possible?

I will include a bit of code so you can see what I'm talking about:

on (rollOver) {
if (_root.EarthClip._currentframe == 1) {
_root.EarthClip.gotoAndPlay(140);
} else { if (_root.EarthClip._currentframe == 160) {
_root.EarthClip.gotoAndPlay(156);
} else { if (_root.EarthClip._currentframe == 161) {
_root.EarthClip.gotoAndPlay(155);
} else { if (_root.EarthClip._currentframe == 161) {
_root.EarthClip.gotoAndPlay(154);
} else { if (_root.EarthClip._currentframe == 163) {
_root.EarthClip.gotoAndPlay(153);

and so on, and so on . . .


Any help would be greatly appreciated.

Thank you in advance.

-MShetler
Re: If/Else - Is there a cleaner code? dr_ross
4/30/2007 8:33:01 PM
That just looks far too epic for an if/else of switch statement by the sounds
of it

The best way would be to do it mathmatically if you could based on the
position of the movieclip that is being moused over. Or have some functionality
whereby when the user mouses over a country, it calls a function passing it the
target frame to stop on. Then apply an onEnterFrame handler to the globe to
keep spinning until it reaches the target frame.



Re: If/Else - Is there a cleaner code? MShetler
4/30/2007 8:44:45 PM
The globe actually stays static until a continent is moused over (and returns
to static when the mouse rolls out of continent). And from a designer's
standpoint I think having the globe constently rotating would not fit.

As for being epic - it is to some degree, BUT maybe not as much as the code
above would have you believe. The longest animation from any one location to
another is only 45 frames long. They average around 20 frames.

I'm not sure I understand what you mean by "do it matchmatically if you could
based on the position of the movieclip being moused over".

Do you mean call out the coordinates of the positions being moused over to
determine where to rotate the globe? If so, that seems similar to what I'm
already doing.

-MShetler
Re: If/Else - Is there a cleaner code? SpaceGirl
5/1/2007 12:00:00 AM
[quoted text, click to view]

Little cleaner...

if ( ) {
//
} else if {
//
} else if {
//
}


Also, there is the "switch" function, which you should look up in the AS
2 dictionary.





--

x theSpaceGirl (miranda)


### future media, video, flash, animation @

# http://www.northleithmill.com


### music industry web & promotion @

# http://www.digitalharmony.co.uk


# Remove NO SPAM to email

# This message must not be reproduced anywhere but Usenet & GoogleGroups

Re: If/Else - Is there a cleaner code? MShetler
5/1/2007 6:31:10 PM
Dave, thanks for the reply.

I'm not entirely sure I follow though. What exactly does the else statement
do? Wouldn't I have to input each of the corresponding frames still, like how
you related 156 to 160? If so, is it just a matter of the bytes being less and
the code running smoother (not to suggest that the code doesn't already run
smooth - as I stated before it works perfectly, I'm just a little nervous about
trying to get the .fla file, which is a little over 2 MB, deployed on a
website).

Thanks in advance for your help!

-MShetler




Re: If/Else - Is there a cleaner code? MShetler
5/1/2007 8:30:00 PM
Additionally, I have several buttons that use the exact same actionscript code.
Is there a way to consolidate this? Right now I have it copied and pasted on
to each button. Can they all just call on the same actionscript stored
somewhere else?

Thanks again!

-MShetler
Re: If/Else - Is there a cleaner code? MShetler
5/3/2007 2:16:43 PM
Re: If/Else - Is there a cleaner code? henrik
5/5/2007 12:00:00 AM
[quoted text, click to view]
You need an algorithm.

Implement this in an actionscript class:
For each frame when you have a destination:
Move one step towards it if you are not already there.

Re: If/Else - Is there a cleaner code? Optikalefx
5/5/2007 2:03:44 PM
orrrr use the wonderful switch statement. its faster. Thats because with if
else flash has to go thru the conditions till it finds a true one, then its
done, with switch it jumps right to the correct condition.

switch(_root.EarthClip._currentframe) {
case 1:
_root.EarthClip.gotoAndPlay(140);
break;
case 160:
_root.EarthClip.gotoAndPlay(156);
break;
case 161
_root.EarthClip.gotoAndPlay(155)
break;

ect..

default:
do something if none of the cases happens
break;
}

Re: If/Else - Is there a cleaner code? ggshow
5/8/2007 12:00:00 AM
try this:



on (rollOver) {
_root.EarthClip._currentframe == 1 ? _root.EarthClip.gotoAndPlay(140) : j;
_root.EarthClip._currentframe>=160 ?
_root.EarthClip.gotoAndPlay(316-_root.EarthClip._currentframe) : j;
}
AddThis Social Bookmark Button