PDA

View Full Version : [AS3] OOP and Simple Animation


mike85
07-06-2009, 07:52 PM
Hi,

I am very new to flash and and as3....

I am making a game using AS3 OOP, thus far i have a static layout created using classes. I have a dice on stage in the form of 6 dice images (showing 1-6...) i have an arrow above dice 1, and i also have a start and stop button

My aim is for when the user clicks start, the arrow randomly moves above each of the 6 dice images until they click stop, thus selecting a number.

I have looked at some tutorials and they mostly seem to create an 'actions' layer on the timeline and use 'enterframe'. I have added an event listener to my start and stop buttons....i was wondering is it possible to have a function in my dice class (Called by the event listener on my start button) that animates/moves the arrow in 6 different locations and stopping when the stop button is clicked - instead of creating a layer on the timeline. Im not sure if this sounds like a stupid question or not tbh....hope it doesnt!

Hope the above is clear, many thanks

rrh
07-06-2009, 10:39 PM
You can change the .x and .y values of the arrow. Probably like:

var value:int = Math.floor(Math.random()*6);
arrow.x=value*50;

DukeW
07-06-2009, 10:45 PM
Hey, I'll give try to explain and hope I make some sense. If you dived in directly to OOP i suppose you are reasonably versed in coding in general. You need to use ENTER_FRAME (or the Timer class) if you want a certain thing to happen continuously (or at least for a while), but if i understood right this is not your case. Let's say we have a class called Dice, and an instance of that class added to the stage with the name dice. In your dice class you define a public function called "public function makeMoves(e:MouseEvent)". In order to make this the handler for an event that is triggered from your document class(lets call your start button "start") you need to add the listener as follows:
start.addEventListener(MouseEvent.CLICK, dice.makeMoves)

mike85
07-07-2009, 01:04 AM
Hi,, Thanks for replying, makes a lot of sense and ill give it a go in the morning!

Cheers,

Mike

kaptain kosher
07-07-2009, 08:31 AM
From your post its seems I wasn't sure if you were familiar with linkage, and I know when I started AS3 and FLash I didn't understand how to accomplish flash programming without extensive framescripting so I'll outline it ...

If you want to make a game completely without (or minimal) framescripting, you use linkage. This lets you write a class definition in a AS3 file and then connect it to a graphic that you drew in flash. You select the picture, right click, then select convert to symbol. Fill in the information than click advanced and select link, export to actionscript, export first frame, and fill in the name of the class. If you have a premade class you leave Base Class blank.

So if you want a player that is arrow keyed control you can write a AS3 class called Player add EventListeners, then draw a player and link it to the class. That way you have a functional player without any framescripting what so ever.

In this particular case you could link the start button and the dice and have the start button call the function of the dice. The problem you encounter here is getting the start button to know what the dice is. With framscripiting all variables on stage can access all other variables on stage, with linkage you have to pass it somewhere in the class definition.

I use the document class to circumnavigate this. Every time you add a child they gain the root variable which refers to the document class. So if you add all your children in the document class every single actor on stage can access every other actor through something like var startButton:StartButton = root.getStartButton();

Its very late right now so if I didn't communicate anything clearly let me know...