Practical way of getting a picture of a library object - actionscript-3

I´m making a level creator for my platformer game on AS3. I´m looking for a way of getting a graphical representation of a library object on my .fla, so the user can insert that object on the world on the desired coordinate. What I mean with this is that I just want a picture of the first frame of the mc, without taking its properties and methods (without the need to make a .jpg containing that image), because that´s when problems begin to appear, and I just want the object functionality on play mode, not the level creator.
Let´s say, for example, a ball that bounces all the time by updating its location every frame. On the level creator I just want to get the ball picture to insert it on the desired location. If I take the whole ball object to the level creator it won´t stop bouncing and it will mess things up.
I hope I´m clear... I just want to know if there is a practical solution to this; if not then I´ll make a class where all the world objects would extend to, that has a init() function that initializes all the object functionality, so it´s called only on play mode and not on level creator. Thanks for reading anyway.

What you're doing wrong is adding functionality directly to the MovieClip.
Please read one of my previous answers that covers this concept in more depth.
MovieClips should be used for the display only, representing the graphics of an actual game class, for example:
public class Player
{
// Graphics that represent the Player.
protected var display:MovieClip;
// Constructor.
public function Player()
{
display = new PlayerMovieClip();
}
}
Anyways, once you fix that you can take a Bitmap sample of MovieClips using BitmapData and its draw():
// Assume that 'mc' is your MovieClip.
var sample:BitmapData = new BitmapData(mc.width, mc.height, true, 0);
sample.draw(mc);
// This is your image.
var texture:Bitmap = new Bitmap(sample);

Related

Convert a symbol to bitmap in Flash using Action Script

I am developing a small game using Flash CC. The question may seem very absurd since I am a novice to coding and Action Script.
Here it goes: Can we write a code to convert a symbol to bitmap?
Actually, the game has multiple objects and I have defined them as buttons. When the user clicks on one of the object, it moves to a new position. I dont want two objects simultaneously to move to a new position.
My logic: If I can make every other object as a bitmap, the user wont be able to click on any other object when one object is moving. Any thoughts???
Yes that is possible.
this code makes a bitmap from your displayObject:
var bitmapData:BitmapData=new BitmapData(symbol.getBounds(this).width,symbol.getBounds(this).height,true);
//The BitmapData Class contains pixels information for a bitmap.I created a bitmap data
//with width and height of the symbol. and set visiblity true.
var bitmap:Bitmap=new Bitmap(bitmapData);
//you know about this !
bitmapData.draw(symbol);
//The draw() method, does what you want.set pixels from a DisplayObject
//and use a matrix in parameters for the rotated,scaled,... form of the DisplayObject.
Now, The bitmap is ready.
I h☺p e this helps !

as3 creating a custom property in Emanuele Feronato's "Flash Game Development by Example"

I recently picked up Emanuele Feronato's Flash Game Development by Example to try and help expand my knowledge of game design and actionscript 3, and I'm stuck on the first chapter where we're building basically a memory match two game where you have ten sets of tiles and you try and match them up by clicking on them.
In the code I'm stuck on, Mr. Feronato is adding the tiles to the stage using a for loop and addChild, here's the code in particular:
// tile placing loop
for (i:uint=0; i<NUMBER_OF_TILES; i++) {
tile = new tile_movieclip();
addChild(tile);
tile.cardType=tiles[i];
tile.x=5+(tile.width+5)*(i%TILES_PER_ROW);
tile.y=5+(tile.height+5)*(Math.floor(i/TILES_PER_ROW));
tile.gotoAndStop(NUMBER_OF_TILES/2+1);
tile.buttonMode = true;
tile.addEventListener(MouseEvent.CLICK,onTileClicked);
}
// end of tile placing loop
In the 5th line, you can see that he creates a custom property of the tile variable called "cardType," but when I try and run the code I get the error "Access of possibly undefined property cardType through a reference with static type Tile." I have the class Tile extending MovieClip, and the main class extends Sprite, but as far as I can tell I've written the code exactly as in the book and can't get past this. I thought about just using a normal int variable cardType to hold tiles[i] but later on you use the cardType property on a mouse event so I'm a little stuck.
Has something changed in Flash that no longer allows you to create custom properties in this way? Or did I just do something stupid that I'm not catching.
As always, thank you so much for the help.
ActionScript supports dynamic classes, in which properties may be added during runtime. Without the dynamic keyword, a class is sealed, meaning its properties may not be altered.
MovieClip is an example of a dynamic class.
Instantiating a MovieClip from code (or MovieClip instance created in Flash Professional), adding this property would be supported:
var tile:MovieClip = new MovieClip();
tile.cardType = "custom value";
However from code, even if extending MovieClip you must add the dynamic keyword:
package {
import flash.display.MovieClip;
public dynamic class Tile extends MovieClip {
/* ... */
}
}
Now, you may add properties to your Tile instance:
var tile:Tile = new Tile();
tile.cardType = "custom value";

Flash Platformer Game changing between two characters

So I'm new to coding as a whole and though I've messed a little with AS before I'm at an absolute loss. I've been using a pretty helpful website that gives a step-by-step on making a platformer/sidescroller, but one of the key things I want to include in my game is the ability to swap between two characters- i.e., if the player hit space or any other key, you could swap out to the other character (with different abilities and all).
My game only really has one big stage, and it's nothing complicated at all (it's something I'm trying to get done in about a little over 24 hours) which is why I decided not to make the character change for each "level"- because there's only one big one. I want the player to be able to decide when they want to play whichever character they fancy, but I have no idea how to go about it.
Uh, and if it helps clarify anything, I basically tried to follow that tutorial site as closely as I could without copying source code.
Thanks so much for taking the time.
You need to do a thing named "separate graphics from code". The tutorial says "convert your player to a Movie Clip Symbol ... create new Player(), add it to stage", then it teaches about how to control the player. You need to split your Player class into two, one remains as Player and contains code, the other is a skin class, barely a MovieClip without code. In your Player class you need to make a method to operate skins, the interface is up to you, I suggest a string lookup of a skin class, then you can tell your player to change skins both at costruction time and probably at runtime. An example:
public static const SKIN_DEFAULT:String="default";
public static const SKIN_RED:String="red";
public static const SKIN_BLUE:String="blue";
private static var SKINS:Object={
SKIN_DEFAULT:DefaultFace;
SKIN_RED:RedFace;
SKIN_BLUE:BlueFace;
}
Here, SKINS is a lookup object with keys as strings and value as Class references, these are the MovieClip symbol names an example expects to exist. Say you want a gray blob, a red angry face and a blue smiley save to be available, you name them DefaultFace, RedFace, BlueFace and put these names into this lookup object. Then, you need to make your player change skins. It's pretty easy once you get the drill.
public class Player extends Sprite {
// Sprite should do, as Player is now code-only
private var skin:MovieClip; // this will hold our player's skin
public function setSkin(newSkin:String):void {
// call this to change the skin
var newSkin:Class=SKINS[newSkin] as Class;
if (newSkin==null) return; // sanity check
if (skin) if (skin is newSkin) return;
// ^ changing skin for the same? We're that skin already!
if (skin) if (contains(skin)) removeChild(skin); // remove old skin
skin=new newSkin(); // create a new skin object
addChild(skin); // make it attached to the player
} // we're done!
// rest of code here
}
Make sure that your Player will at least put on default skin when created, or else you won't be able to see player move.
In order to trigger skin change, you can create an array of buttons, up to making them out of all possible SKINS (for this, either expose SKINS variable by declaring it public instead of private as it's now, or provide another means to get the list of classes), which will call player.setSkin() with a correct parameter.

Replacing movieclip

I m currently working on converting one of my as2 game project into as3 (flash pro cs4). But there is a problem on the background movieclip.
I have a function called disposemc:
function disposemc(mcname:MovieClip):void{
mcname.parent.removeChild(mcname);
mcname = null
}
Another function called changelayer:
function changelayer(mcname:MovieClip,layer:MovieClip):void{
layer.addChild(mcname)
}
So in main timeline frame 2 I have a movieclip on stage (placed in IDE) with instance name "bg_mc", then on main timeline frame 3 I have a DIFFERENT mc on stage with the SAME instance name "bg_mc". The purpose of this is to replace the old bg_mc by the new one.
The problem is,
In frame 2, I applied changelayer function to bg_mc and moved it into a child mc of root by addChild, and then applied a function of my CPU image post processing framework and added the bg_mc into an array.
After some stuffs happened, I went nextFrame to frame 3, and found that the new bg_mc didnot replace the old one, instead it overlaps.
So I tried disposemc function when leaving frame 2 to get rid of the old bg_mc, and at frame 3 I applied changelayer to bg_mc and added bg_mc to my render array.
And I found that the old bg_mc is off the stage but it is still rendering onto my bitmap data, means it is not nullified like it suppose to be in disposemc function. And it is also hard to access it because the name overlaps. When I call bg_mc in frame 3 it is the new one, but the old one still exists, and from the rendered bitmap data it can see it is still playing.
Will making the old mc stop in disposemc function help?
Can anyone give any help? If my structure of making bg is bad, is there any other way to override an instance with a different mc in library?
The purpose of this is to replace the old bg_mc by the new one
You work with MovieClip and frames. So use keyframes by design. If you need to place different background in third frame, don't program it, place it. If you want program, don't use MovieClip with keyframes as main navigation instrument.
Main timeline could be easily swapped with very simple logic and several movie clips with only one frame
//Some MovieClips from the library, with only one frame, still designed in Flash IDE
var currentScene:DisplayObject;
var home: MyHove = new MyHove()
var intro:MyIntro = new MyIntro();
navigate(home);
//after some interaction from the user, go to another page
navigate(intro);
//It will work like gotoAndStop... but better, and give you more control
function navigate(scene: DisplayObject):void{
if(currentScene != null){
removeChild(currentScene);
}
currentScene = scene;
if(currentScene != null){
addChild(currentScene);
//apply logic for swapping background
}
}

Assign integer values to movie clips in ActionScript 3

I am trying to make a card game in Flash using ActionScript 3. I'm not too familiar with the language(grew up with AS2) but I considered it to be more appropriate for this project.
Bad thing about this, though, is that I ran into a problem right away: I'm treating each individual card as a movieclip, but something that I really need is to assign some integer values to the card(It's not really the case, but as an example let's say that I am working on a Poker and I want all Aces to carry the value 1 because they are the best card, 2 for the kings, etc).
I tried looking for it but all I found is how to make arrays of movieclips. I know that this question shouldn't be too hard! Can somebody help me?
(As an aside note, should I really work with movie clips or would buttons be more convenient?)
Firstly, from my point of view, never use components unless you really need to since they take longer time to draw and they're not flexible. I've had so many issues with them in the past (this question I posted being one of those).
About MovieClip's… The MovieClip class is dynamic so you can assign any virtual property to it, no problem. So yourMcInstance.someVar = 3 is perfectly valid.
One of the major changes in AS3, I think, was the introduction of Sprite class, which is basically a MovieClip without a timeline. It is much lighter and unless you're manually creating frame animations it is the class to choose for any container that can handle mouse (and many other) events. However, it is not a dynamic class so yourSpriteInstance.someVar = 3 wouldn't be valid. And that's why using custom classes is encouraged. With custom classes you have the option to extend a class and create custom properties and methods.
because Movieclips are Objects, you can actually attach variables directly to them.
var card:MovieClip = new MovieClip(); //create a blank MC
addchild(card);
card.id = 5; //You can attach vars like this
To add MovieClips to an Array:
const clips:Array = [];
function addNewCardToCardsArray(array:Array, color:String, value:int):MovieClip {
const clip:MovieClip = new MovieClip();
clip.color = color;
clip.value = value;
return array[array.length] = clip;
}