Pan class in Flash AS3 - actionscript-3

I have a movieclip and I am going to add a pan feature to it. I want to add the pan as it's own class that the movieclip can reference.
When I create the pan class, I send the movieclip to it so I have it's position properties at all times.
What is the best way to access the stage in the pan class? I will need to be able to get the stage mouseX, mouseY, stageWidth, and stageHeight.
Right now, I have the pan class extend a sprite object and actually add it as a child of the movieclip I want to pan.
Would it be better to just send the stage itself into the pan class as well or is there a better way than this?

Create a singleton class that manages changes to the stage called StageManager and initialize it by passing it a reference to the stage:
//StageManager.as
import flash.display.Stage;
public class StageManager()
{
//publicly accessible singleton instance
public static var instance:StageManager = new StageManager();
private var m_stage:Stage;
//I'm using a custom getter and setter in just in case you need perform some other
//initialization when the stage gets set...
public function set stage(stg:Stage):void
{
m_stage = stg;
}
public function get stage():Stage
{
return m_stage;
}
}
Then somewhere else, like you main controller class:
StageManager.instance.stage = this.stage;
Now you can access the stage and its properties globally through the StageManager:
var stageW:int = StageManager.instance.stage.stageWidth;
This way, not just you Pan class, but anything else down the road can access the global stage any time it needs to. Pretty cool, huh?
As for how to design your Pan class, I agree with #The_asMan - extend MovieClip. Unless, that is, one Pan instance will be controlling several MovieClip instances, then it would probably make more sense to have it as its own thing (the way you describe above).

Related

AS3: How do I access a variable from a parent object?

I'm new to AS3 and my code may look a bit off.
Basically, how my program works is that I have an instance of "Level" on the stage, and it is a MovieClip containing several other objects which are also MovieClips with their own document class. In the "Level" class, I can access the X and Y position from the instance of "Player", but in my "Arrow" class, which is also a child of Level, I'm unable to access the X and Y of "Player". What I tried to do is set a public static variable called playerX and playerY in the Level class and assign that to the player's x and y position every frame, and I try accessing the variable in the Arrow class by doing "var x:Number = Object(parent).playerX, I've also tried MovieClip(parent).playerX and parent.playerX and just player X, neither of them work.
So to sum it up, I need to access a variable from a parent class, but every way I have tried it just returns an error.
Sorry if this was a bit unclear, any help will be much appreciated!
Sounds like you are using FlashPro Timelines, and have two siblings objects (they have the same parent MovieClip/timeline) and you want to be able to reference one from the other (let me know if this doesn't properly summarize your question).
Since it sounds like you have class files attached to your Arrow and Player classes, you'll want to make sure any code referencing the parent or stage or root vars is run AFTER the item has been added to the display list as those vars are only populated after the item has been put on the stage. (this isn't an issue for timeline code).
So, something like this:
public class Arrow extends MovieClip {
public var player:Player; //a var to hold a local reference to the player
public function Arrow():void {
//right now parent/stage/root are null
//listen for the added to stage event, so you know when it's safe to use the parent and stage and root keywords
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(e:Event):void {
player = MovieClip(parent).playerInstanceName;
//where player instance name is the instance name of your player on the parent timeline
}
private function doSomethingWithPlayer():void {
if(player){
player.x = 5;
}
}
}
Now, if you wanted to use a Static var instead (might be less cumbersome than the parent keyword), and there is only ever 1 player instance, you could do the following:
public class Player extends MovieClip {
//create a static var to hold a reference to the player instance
public static var me:Player;
public function Player():void {
me = this; //now assign the instance to the static var.
}
}
Now, you can access the player instance from anywhere in your app by doing Player.me (regardless of context)
Player.me.x = 5;
You need to understand what a static variable is and what it is not. A static variable IS a Class variable, A static variable IS NOT an instance variable. So if I create a static variable called "playerX" in a class called "Level" I can now do this:
Level.playerX = 0;
Because Level is a class and playerX is a variable of that class.
I cannot do this:
Object(parent).playerX
Because 'parent' is not a class and does not have a variable called 'playerX', parent might be an instance of the class 'Level' but instances of the class 'Level' do not have a variable called 'playerX', the class itself "Level" is the one that has that variable.

Making a simple tamagoci game getting no compiler errors but receiving no output

Kind of new Actionscript and I'm just trying to make a simple tamagoci game. I've wrote all the code out but and receiving no compiler errors but for some reason I'm also not receiving any output messages for my mouse event listeners. Here is all my code, I really can't find the problem and any help would be greatly appreciated. Thanks.
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip{
public var feedButton:MovieClip;
public var tamagoci:MovieClip;
public var disButton:MovieClip;
public var dietButton:MovieClip;
public function Main() {
this.init();
}
private function init():void {
this.feedButton.addEventListener(MouseEvent.MOUSE_DOWN, onfeedMouseDownHandler);
this.disButton.addEventListener(MouseEvent.MOUSE_DOWN, ondisMouseDownHandler);
this.dietButton.addEventListener(MouseEvent.MOUSE_DOWN, ondietMouseDownHandler);
}
private function onfeedMouseDownHandler(event:MouseEvent)void{
this.tamagoci.scaleX += 0.1;
this.tamagoci.scaleY += 0.1;
}
private function ondisMouseDownHandler(event:MouseEvent)void{
this.tamagoci.gotoAndPlay(5);
}
private function ondietMouseDownHandler(event:MouseEvent)void{
this.tamagoci.scaleX -= 0.1;
this.tamagoci.scaleY -= 0.1;
}
Are you using Flash Professional?
You're declaring your variable types in your class here;
public var feedButton:MovieClip;
public var tamagoci:MovieClip;
public var disButton:MovieClip;
public var dietButton:MovieClip;
But then in your constructor, all you are doing is running init();
public function Main() {
this.init();
}
So, this could one of a few things. The most likely is that you have declared your variables, but you haven't initialised them. You've created the variables to hold your objects, but according to your code, they're empty. More specifically, a variable or class property that doesn't assign an object to a variable of an object type will contain a default value of null.
You could prove this in your code by simply putting a condition inside your init(); method;
if(tamagoci == null){
trace("I haven't been assigned an object of type class yet!")
}
So it could be 1 of these 3 things:
1: If you have written your own classes for these class properties/variables, then you need to instantiate them with the new keyword. The general syntax is;
variable_name = new ClassName(parameter_1, parameter_2);
If you are using classes you have written yourself, you have to create an instance of the object, assign it to a variable, and then add it to the stage using addChild();. For example, lets say you've written your own Tamagoci class;
tamagoci = new Tamagoci();
tamagoci.x = 100; // set the x location
tamagoci.y = 200; // set the y location
addChild(tamagoci);
Notice the use of Tamagoci. This is just an example, but this is the class name, which shouldn't be confused with variable/property name. It could just have easily been;
tamagoci = new MovieClip();
But then, this is just an empty MovieClip. It needs a property to display on the screen. A Shape, A Bitmap, or another container class object like MovieClip or Sprite (container classes allow you to nest display objects inside them). But on a basic level, it must contain a visual component to appear on the stage.
2:
Have you made Main your document class? This is the class which will get automatically called when your Flash movie plays. To set this, click on your stage, and in the properties dialogue box on the right, under PUBLISH, type in the name of your class, which is "Main".
3:
If you have created MovieClips in your library in Flash Professional, then you need to go to your library, right click the MovieClips, and select properties. From there, you need to make sure Export for Actionscript is ticked.
Now, if you click on your MovieClips on the stage, then open the Properties tab in the top right of Flash Professional's default layout, then right at the top should be a text field, and if you hover over it, Instance name will pop up as a tool tip. This is where you name your stage objects. Once that is done, you have access to them in your timeline.
If this is how you've done this, then you don't need to declare the variables in your main class, as they are already declared on your stage by Flash Professional and instantiated automatically.

Error with hitTestObject

I am trying to make a game similar to the world's hardest game, but I have trouble with the hitTestObject block. This is my code for the enemy mi:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class enemys extends MovieClip {
public function enemys() {
stage.addEventListener(Event.ENTER_FRAME, hittrue)
}
public function hittrue(event:Event) {
if (this.hitTestObject(?)) {
while (numChildren > 0) {
removeChildAt(0)
}
gotoAndStop(2)
}
}
}
}
I don't know what to put into the question mark. When I put the instance name of my player, it says that it is undefined.
You're getting an error because enemys (sic) doesn't appear to have access to any sort of player instance.
You should move the hit testing out of the enemys class to somewhere you have access to both enemys and the instance of player. A good place for this would be some kind of GameEngine class.
How do you put the instance name of player? Do you pass the instance through the constructor?
From you main class you need to send a instance of your player to the enemys class.
(by the way plural for enemy is enemies)
public class Enemies extends Sprite{
private var player:PlayerClass;
public function Enemies(p:PlayerClass) {
stage.addEventListener(Event.ENTER_FRAME, hittrue);
player = p;
}
Then in you can put 'player' where the ? is. And in yout main class you would have something like:
var enemies:Enemies = new Enemies(player);
I changed MovieClip to Sprite. This is your choice but it is sometimes better to use Sprites because it will be faster then MovieClip. You may want to look into them especially if you are going to have multiple enemies on the stage at a time.
Another thing is the design of you ENTER_FRAME event.
You do not want multiple Enter_Frame events going on in multiple classes. A good design is to have one in your main class. Then from classes that need a clock cycle, call update methods on these objects in the main class's ENTER_FRAME event.
so in your main class's ENTER_FRAME event you would call:
enemies.hittrue();
Instead of having an EnterFrame event in you enemies class. This will also make it much easier to pause your game.
And as The other answer suggests. Your Collision detection should really be outside of your Enemies Class. But, this is how you would pass a reference of you player to another class.

AS3 - How can I change contents of all the same Movieclips runtime?

So, basically, what I want to do is replace the contents of a sword MovieClip that's inside a Player MovieClip already, and is animated, so it has multiple instances of the sword MovieClip across the Player MovieClip.
Can I somehow edit the contents of the sword MovieClip in actionscript so as all the sword MovieClips update and are changed?
What I want to achieve is just changing weapons of a character animation that doesn't require me to await every frame and removeChild() the previous weapon and addChild() the new one of every instance of the weapon.
I'm not sure I'm getting it but Maybe you need a weapons event class
package WeaponEvents{
import flash.events.Event;
[Event(name="sword1", type="event.sword1")]
[Event(name="sword2", type="event.sword2")]
public class SwordEvent extends Event
{
public static const SWORD_1 : String = "sword1";
public static const SWORD_2 : String = "sword2";
public var arg:*;
public function SwordEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, ... a:*) {
super(type, bubbles, cancelable);
arg = a;
}
// Override clone
override public function clone():Event{
return new LoadEvent(type, bubbles, cancelable, arg);
}
}
}
Just add the event to when your user chagnes weapons.
I'm in a similar boat as you. Except I have 30-50 bodyparts to cover, so I really can't go the brute force way.
If it's just sword that you want replace though, I assume in the animation it is on its own layer? Then a "cheap way" I found is:
Copy paste all frames in that layer (and only that layer) into another movieclip i.e. Sword1Swing.
Remove that layer (and only that layer) from your MC animation.
In yet another movie layer, put Sword1Swing on frame 1, Sword2Swing on frame 2, etc.
MC.gotoAndStop("SwordSwing"); SwordMC.gotoAndStop("Sword1Swing");
As long as you keep the .x and .y of the SwordMC synchronized with MC, the animation should always line up.
Hand often goes over the top of the sword, in which case... You can copy the hand too. Or the whole arm. Or you can custom shape your sword symbol's basic graphics in the library to match exactly with the empty spaces between fingers (copy and paste hand graphics in place to remove unwanted portions, then delete the hand graphics, then you'll have the perfect shape).
It's the ghetto way I know, I am very much in need of an actual efficient swapping. But this may help you to get through the project fast.

Stage and classes

I am new to AS3 and am trying to lean its OOP ways. What I am having problems with is understanding how to access the stage with separate classes.
Here is an example of what I am trying to do:
package game{
import flash.display.*;
public class Main extends MovieClip{
function Main(){
var player = new Player();
var playerBullets = new playerBullet();
addChild(player.players);
}
}
package game{
import flash.display.*;
public class Bullet extends Main // also tried with MovieClip and Sprite{
function Bullet(){
// empty
}
function blah(){
var someSprite = new someSprite();
Main.addChild(someSprite);
stage.addChild(someSprite);
root.addChild(someSprite);
}
}
}
I have Omitted another class which calls the blah method as I feel it is not relevant.
Basically what I want to know is how to add things to the stage in classes as it lookes like I am missing something crucial.
*EDIT TO INCLUDE ERROR*
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game::Bullet/blah()
at game::Player/fire()
You shouldn't necessarily be extending main to create something like a bullet class, this can be it's own class that extends Sprite or MovieClip. The stage object is considered a global object, as it is a singleton (except in the case of Adobe AIR where you can have one stage per NativeWindow that you spawn). So any object that extends DisplayObject or has DisplayObject in it's inheritance chain will by default have a reference to the stage via a getter, which is populated automatically when a displayObject is added to the display list. This can happen by either adding a clip directly to the root stage object or by adding a clip as a child of another clip, that eventually connects to the stage. For example:
var clip1:MovieClip = new MovieClip();
stage.addChild(clip1); //Clip 1 can now access the stage reference internally.
ver clip2:MovieClip = new MovieClip(); //Right now, clip2 cannot access the stage reference interally.
clip1.addChild(clip2); //Now clip2 can access the internal stage reference because it has been connected to the display list through clip1.
The other mistake people make is accessing stage within a DisplayObject typed class (such as your Main class) without first ensuring that the object itself has been added to the stage. You do this by listening for the Event.ADDED_TO_STAGE event within the constructor of the class, like so:
public class Main extends MovieClip{
function Main(){
if(stage){
//The stage reference is present, so we're already added to the stage
init();
}else{
addEventListener(Event.ADDED_TO_STAGE, init);
}
var player = new Player();
var playerBullets = new playerBullet();
addChild(player.players);
}
private function init(e:Event = null)
{
trace("Added to stage, the stage reference is now populated and stage can be accessed");
}
}
This could be the problem you're having, but it's hard to say since you have not specified any errors. However, this is likely an issue or will be for you, since it's quite common. Inside the init() method you can then set a flag so that when external classes call your Main.blah() method, you can ensure that the stage reference exists before attempting to add something to the stage. Take note however that within your Main class when you simply say:
addChild(someChild);
or
this.addChild(someChild);
you're not adding that child to the stage, but rather to the Main object, which is a MovieClip or Sprite based object that is itself attached to the stage automatically when you set it as the Document class. Hope this info helps.
Update
To explain the display list a little more:
Think of all your movieclips as dishes, and the stage as the table. You can only access the table from the dish, if the dish is placed directly on the table, or if a dish is stacked on top of another dish that touches the table. If you have 10 plates stacked on top of each other, they all touch the table eventually, via their connection to each other. This is essentially a visualization of the flash display list. The way you put dishes on the table is by using addChild(dish). If you have not placed an object somewhere on the table, and try to access the table from that object, you're going to fail. You're getting the "access to undefined" error because you're calling the "blah()" method, which accesses the stage (table) before the bullet (dish) has been added to the stage (table). So you must first either directly add the bullet to the stage, or add it to another object that has already been added to the stage. Change your code like so:
var myBullet:Bullet = new Bullet();
stage.addChild(myBullet);
//Or, if this class, assuming it's the player class, has already been added to the stage, just do this:
this.addChild(myBullet);
myBullet.blah();
Even so, you should still have some error checking within your "blah" method to ensure that the stage is available:
function blah(){
var someSprite = new someSprite();
if(stage){
Main.addChild(someSprite);
stage.addChild(someSprite);
root.addChild(someSprite);
}else{
trace("error, stage not present");
}
}
However you should also note that by adding this child to Main, then stage, then root all in sequence, this does not duplicate the someSprite object. When you add a display object to a new parent object, the object is automatically pulled from it's current parent and moved to the new one. So all this code will do is eventually add someSprite to root, which I believe will fail because root is not a display object, but rather a global reference mainly used to access global objects such as the stage and the Loader object used to load the SWF.
You shouldn't ever be calling stage.addChild. There should be only one child of the Stage, and that's the document class.
You make a MovieClip display on the screen by adding it to the stage's display list.
Stage
+ Main Timeline
+Anything
+Else
+You
+Want
So assuming that Main is your document class for the main timeline...
// inside of Main's constructor...
public function Main(){
var anything:MovieClip = new MovieClip();
var Else:TextField = new TextField();
var you:SimpleButton = new SimpleButton();
var want:Sprite = new Sprite();
this.addChild(anything);
this.addChild(Else);
this.addChild(you);
this.addChild(want);
}
Then in order to add children even lower, for example if you want something to be a child of "Anything" such that you have....
Stage
+ Main Timeline
+Anything
+And
+Everything
+Else
+You
+Want
public function Main(){
var anything:MovieClip = new MovieClip();
var Else:TextField = new TextField();
var you:SimpleButton = new SimpleButton();
var want:Sprite = new Sprite();
this.addChild(anything);
this.addChild(Else);
this.addChild(you);
this.addChild(want);
var And:Sprite = new Sprite();
var everything:Sprite = new Sprite();
anything.addChild(And);
anything.addChild(everything);
}
EDIT: Ascension Systems asks why you should never add any display object directly as a child of the stage. The simplest answer is that you can't ever guarantee that what you believe you're creating as a document class, or as a main timeline in fact actually is going to be used as such. Your use of the stage may later preclude your swf from being loaded as a child of a larger application depending on what it is you've done, exactly. Relying directly on the stage can mean that you're making some assumptions about the nature of the display list that may not hold in the future. That's the way in which it breaks modularity (which is not the same as breaking oop).
Why add to the stage when you could just create your entire application as a MovieClip that is completely self-contained with no reliance on the concept of a "stage" beyond that which is required for learning world coordinates? That way you can be much more modular in your design and you sacrifice nothing.
In some people's work this may be considered an edge case. In my work this has happened both to me when I've created applications that I thought at the time were purely stand-alone that ended up being repurposed later to be a module, and also to swfs that other people created that were intended to be strictly stand-alone, but that I was then to integrate as a module into a larger application. In all cases there were some nasty side effects to contend with. That's where I learned not to rely too closely on the stage for much beyond world coordinates.
Every display object has a property called stage, which is null until that object is added to the display tree.
When you are unsure if an object has been added to the stage, there is a listener you can employ for that purpose:
public class Main extends MovieClip
{
import flash.events.Event;
public function Main():void
{
if(stage) {
init();
} else {
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
}
private function init(evt:Event = null):void
{
this.removeEventListener(Event.ADDED_TO_STAGE,init);
//object is now definitely on the display tree
}
}
I'm gonna take a wild stab in the dark here.
stage is a property implemented something like so:
public function get stage():Stage {
var s:DisplayObject = this;
while(s.parent) s = s.parent;
return s as Stage;
}
root is very similar but stops a level below stage (root is a child of stage).
These properties only work when the object you're calling them on is on the stage somewhere. Doesn't matter where, because the while loop will walk up the hierarchy to get to the stage node at the top. But if it's not on the stage, then parent will be null.
So if your movieclip is not on the stage, then its reference to stage will be null. Same goes for root.
I'm guessing that you're calling blah before the bullets are added to the stage? In which case your call stage.addChild(someSprite) will be a Null Reference error (stage is null).
So you either need to add the bullets to stage first, or you need to pass stage in as a parameter:
function blah(s:Stage){
var someSprite = new someSprite();
s.addChild(someSprite);
}