Using instances already on stage from another class
I'm new to this OOP stuff, and I'm having a hard time understanding all of this.
I'm trying to recreate in AS3.0 with classes a simple whack-a-mole game I created in AS2.0 using timeline coding.
I've read through a lot of forums, but I still don't understand what exactly I'm doing wrong.
Heres my setup:
- I have a movie clip named
mrBunny
(my girlfriend told me to change it to bunnies as moles were too ugly.). Now there are 6 instances ofmrBunny
on the stage, each namedmrBunny0-5
. - The
mrBunny
symbol is linked to thecom.mrBunny
class. - The cl开发者_StackOverflow中文版ass has a method called
randomPlay();
which I use to randomize the animation times ofmrBunny
. - I also have a button on the stage with the class
stageBtn
.
package com{
import flash.display.SimpleButton; import flash.display.MovieClip; import flash.display.Stage; import flash.events.MouseEvent; public class startBtn extends SimpleButton { //Constructor public function startBtn() { this.addEventListener(MouseEvent.CLICK, startClick); } public function startClick(event:MouseEvent):void { mrBunny0.randomPlay(); mrBunny1.randomPlay(); mrBunny2.randomPlay(); mrBunny3.randomPlay(); mrBunny4.randomPlay(); mrBunny5.randomPlay(); } }
}
I want to be able to use the startBtn to start the animation of the mrBunny# instances.
As far as I am aware, I'm not fully grasping the situation of classes and OOP.
(The bunnies were just too cute to pass on this question, so here goes...)
There are several problem areas to consider in your code about OOP. I will try to explain them a bit.
Package Names:
Package names are given in order to uniquely identify a class. Consider the situation when you are using a 3rd party library, which has a StringUtils class. You also happen to have a StringUtils class yourself. How do you store them in your work folder? How do you address each of them in your code?
Packages, as the name implies, provide a mechanism to group classes. So when you refer to your class you might address it as com.joemidi.utils.StringUtils
and the other as com.someoneelse.utils.StringUtils
. Package names can be anything you like, as long as it reflects the folder structure. But, as an industry standart people use URLs in their package names, as they are guaranteed to be unique. This is the reason you see com
in many packages. In your situation it is better if you restructure your packages (and folder structures) according to this.
Stage Instances from the IDE:
When you create instances in the Flash IDE, you must remember where you put them and whether your code knows about them or not. The bunnies, as @weltraumpirat said, are not inside your startBtn. The proper way to do what you are trying to do is this:
Main:
+ contains the bunnies.
+ listens to startButton for MouseEvent.CLICK
+ when startButton is clicked, manipulates the bunnies.
That said, we realize there is another problem:
Don't Rely on Instance Names:
You should instantiate (that is, create new) bunnies in your code, not in the Flash IDE; and access them from a central variable. For example:
public class Main extends MovieClip {
var bunnies:Array = new Array();
public function Main() {
createBunnies(7);
startButton.addEventListener(MouseEvent.CLICK, onStartClicked)
}
protected function createBunnies(bunnyCount:int):void {
for (var i:int = 0; i < bunnyCount; i++) {
var bunny:Bunny = new Bunny();
addChild(bunny);
// configure bunny.x, bunny.y, etc. here.
bunnies.push(bunny);
};
}
protected function onStartClicked(e:MouseEvent) {
for (var i:int = 0; i < bunnies.length; i++) {
var bunny:Bunny = bunnies[i];
bunnies.randomPlay();
};
}
Here, you are no longer bound to what instance names you gave to the bunnies. (Of course, I'm just assuming your stage structure here.) And this way, the bunnies are more "independent" of the code above them. Also, you could use a holder sprite and track the bunnies from there, but it might be a bit advanced right now.
Hope these may prove useful to you. If you are serious about this subject, you might like to read more on OOP, specifically on why it is needed, and key terms about it: decoupling, inheritance, encapsulation, etc.
startBtn doesn't have members mrBunny0-5, the main timeline does. Try root.mrBunny0
instead.
精彩评论