SpriteSheet Class AS3 - actionscript-3

I want to do my own SpriteSheet Class for ActionScript 3.
The purpose of this class would be a replacement for the MovieClip class, the only difference is that the frames will come from a SpriteSheet. Many of my classes will extend from this class.
My problem is: What if I had a sub-class, for example Ball, that uses the same SpriteSheet for all instances of Ball and i would like to re-use that SpriteSheet to use in all balls to save a lot of memory.

If you want to share the same SpriteSheet for all instances of Ball, your Ball class shouldn't extend from SpriteSheet class. The base class should contain a variable of SpriteSheet type, and be initialized when the class is created.
So you need a singleton class to save all the SpriteSheet classes with a dictionary, the key of dictionary may like "Ball", "MAN_RUN".The SpriteSheet class will contain a list of BitmapData.
The code may like this
public class Animation {
private var _spriteSheet:SpriteSheet;
public function Animation(key:String)
{
spriteSheet = SpriteSheetMgr.instance.getSpriteSheet(key);
}
}
private var ball:Ball = new Ball("Ball");

Related

Can't cast embeded MovieClip to MovieClip type

I'm having trouble trying to embed a MovieClip in an ActionScript file I'm composing in FlashBuilder.
public class ItRock extends Item
{
public static const ID:String = "rock";
[Embed (source="/../art/menu/console.swf", symbol="itRock")]
private var IconClass:Class;
public function ItRock(game:Game)
{
super(ID, game);
var icon = new IconClass();
// var icon : MovieClip = new IconClass();
// var icon : MovieClip = new IconClass() as MovieClip;
addChild(icon);
}
}
My console.swf file contains a symbol called itRock which is of type MOvieClip and set to Export for ActionScript. In my code, I want to create an instance of this symbol and add it as a child of my Item class(which extends Sprite). However, when I create an instance of the embedded class, I create an object with the type name of console_swf$831ea9c30fe7882fadc388b74e115654-652499362. I can add it as a child fine, but if I try to cast it to a MovieClip implicitly I get an error that cannot be converted to a MovieClip. If I try to cast explicitly, I just get null.
Any idea what I'm doing wrong here?
Okay, it looks like Flash will automatically convert your MovieClips to Sprites when it can. So my above code will work if I change the MovieClip classes to Sprite.
The below website describes this. It suggested that adding an extra frame to your symbol will force Flash to treat is as a MovieClip and not a Sprite - however, I've created a simple two frame animation and its still exported as a Sprite. (It even plays the animation - I thought Sprites were supposed to be just static images with no animation?)
http://chrismweb.com/2011/03/20/problems-with-embedding-swfs-in-actionscript-or-flex/

Purpose of class extends MovieClip AS3

I have just started learning AS3 and my school has provided some Class files with the basic structure already written. Here is an example:
package {
//Add in your import statements here
//...
public class MathsQuiz extends MovieClip
{
//Add in your class variables here
//...
public function MathsQuiz()
{
}
public function startGame()
{
//Get the game loop to execute
addEventListener(Event.ENTER_FRAME,update);
}
public function update(evt:Event)
{
//This is the game loop
//Handle user input
//Handle game logic
//Handle display
}
}//end class
}//end package
My Question is, what does "extends MovieClip" actually do?
Thank you for your time!
The MovieClip class inherits from the following classes: Sprite, DisplayObjectContainer, InteractiveObject, DisplayObject (can be added to the display list, moved around via its x and y properties as Marcela said), and EventDispatcher, but unlike the Sprite object, a MovieClip object is Dynamic (A dynamic class defines an object that can be altered at run time by adding or changing properties and methods. A class that is not dynamic, such as the String class, is a sealed class. You cannot add properties or methods to a sealed class at run time.) and has a timeline.
If your class doesn't use timeline (looks like that is the case), you can extends Sprite and will have the same results and better performance. If you want a dynamic class you can just use the dynamic attribute when you declare a class.
The extends keyword allows a class to inherit any publicly-accessible or protected member variables and functions of a base class (in this case, MovieClip).
In this example, this means that MathsQuiz, on top of any functionality you may add, will also function as a MovieClip. On a basic level, this means that it is a display object that can be added to the display list and moved around via its x and y properties.
For a more in-depth understanding, do some research on OOP Inheritance.

In Flash Pro, how to get the Properties panel to expose the properties you define on your ActionScript class?

I'm dabbling with game design and trying to create some characters for the game. Right now I've just created a single MovieClip that contains a rectangle. The MovieClip symbol extends a class that I've created in Flash Builder that implements the logic of a monster. I can then drag an instance of this monster symbol from the library to the stage and the code works when I run the simulation. So far, so good.
Now I want to create several monsters, all slightly different:
public class Monster extends MovieClip
{
public var isFriendly:Boolean = true;
public var strength:int = 10;
public var catchPhrase:String = "Booyah!";
public function Monster()
{
}
}
One way to do this is to write a new class for each monster that extends Monster and sets the properties I want in the constructor (I'd also have to create a unique symbol in the library for each of these variations too). However, this seems to be overkill if my monsters only differ by their property values.
Looking at the Flash Professional use interface, I see that at the very bottom of the Properties panel is a section that looks like a small table headed by 'Properties/Value'. Can I use this to somehow set the properties of my classes from within the Flash Professional UI? I can't find any info on how this is used.
Okay, I figured it out. The key is converting my symbol into a flash Component.
First I edited my ActionScript class to export the properties I wanted to set (including the Inspectable tag):
public class Monster
{
private var _catchPhrase:String;
public function Monster()
{
}
public function get catchPhrase():String
{
return _catchPhrase;
}
[Inspectable(name = "catchPhrase", type = String, defaultValue = "Booyah!")]
public function set catchPhrase(value:String):void
{
_catchPhrase = value;
}
}
Then I right clicked on the Monster symbol in my library and selected 'Component Definition...'. This brought up the Component Definition dialog. I then entered the name of my ActionScript class in the Class field and clicked the checkmark to validate it. Flash then automatically generated the properties I needed.
I also found this tutorial helpful:
http://redbjarne.wordpress.com/actionscript-3-0-custom-components-from-hell/

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.

Pan class in Flash AS3

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).