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 !
Related
I'm building a spritesheet class. Hopefully this will be low hanging fruit to someone, but I'm stumped.
I have a spritesheet (.png) that I've loaded at runtime and placed a section of it on the stage using this code from within the Spritesheet class .as file ouside of the constructor method:
private function onLoad(e:Event):void
{
var loaderBmp:Bitmap = Bitmap(_loader.content);
_bmpData.copyPixels(loaderBmp.bitmapData, new Rectangle(0,0,80,80),new Point(0,0));
}
That works fine. I get my slice of the .png file displaying nicely. In my case, the spritesheet is meant for animating a character, so I need to update the BitmapData and I'm not having any luck. Here is what I'm trying this within my Main class in a function I use to alter the frame of the animation depending on the state of the character:
c._thisSpriteSheet._loader.content.bitmapData.copyPixels(loaderBmp.bitmapData, new Rectangle(0,20,50,30),new Point(0,0));
loaderBmp is a variable who's value is var loaderBmp: Bitmap = Bitmap(_spriteSheet._loader.content);
c is a reference to the Runner object that is the character.
_spriteSheet is a property of the Runner class of type Spritesheet.
_loader is a property of the c._spriteSheet and is the Loader object used when the spritesheet was instantiated.
It doesn't throw an error, but it also doesn't replace the original bitmapData object with the new one. I thought maybe this meant that I need to create a new BitmapData object and use that in the copyPixels method, but that returned the same results (nothing). When I step through the code in debug mode, everything looks like it is working, but my display object does not update with the new bitmapData. What am I tripping on?
Thanks for looking!
Well, probably no one will read this since I'm answering it so quickly, but I literally spent 3 days trying to figure this out. Sometimes trying to ask the question in a concise way helps one answer their own question, and moments later, voila!
So in case anyone has a similar issue, what I was doing wrong was that I was trying to access the BitmapData object via the Loader that originally loaded it. Then it dawned on me that I could simply reference the BitmapData directly via that property of the SpriteSheet class I had made. I think this will be pretty confusing for someone else to follow. If a moderator sees this and thinks it's junk, I don't mind it getting erased, but thought I'd keep it up anyway. The new code looked like this:
c._thisSpriteSheet._bmpDSheet.copyPixels(loaderBmp.bitmapData, new Rectangle(0,20,50,30),new Point(0,0));
and _bmpDSheet is the bitmapdata property of the class.
I'm making a game for a school project. The concept is pretty simple, you need to fix the circuit wires(the path in the form of rectangular shapes) so that electricity can run through the circuit and light up the bulbs.
I'm trying to find a way on how you can make a movie clip travel the wires. I have seen a lot of tutorials where they state coordinates and angles for the motion path but I want to make it so that the movie clip will automatically follow the shape path present on the stage so even if the path changes(for different levels), the movie clip will still follow that path. As of the moment, all I can do is create a predefined guide path inside the movie clip which traces the path.
Follow-up question:
Is there also a way on how I can detect if the shape path is complete or not? The system will check whether or not the wires are connected to each other.
You should instead do a metadata calculation prior to moving something you intend, this way you first calculate whether your puzzle is solved, once it is, or once you have determined the point where your movable object should stop and display the error, you then make a path out of simple sections, then make your object move by them one by one until final point, then display the outcome. This is the general idea. A simple code of moving an object by sections looks like this:
var sections:Vector.<Point>; // we need x&y sequence. Fill this prior to launching the routine
var position:int=0; // where are we now
var velocity:int=8; // pixels per frame, adjust as needed
movable.addEventListener(Event.ENTER_FRAME,moveABit);
function moveABit(e:Event):void {
var nextPoint:Point=sections[position];
var do:DisplayObject=e.target as DisplayObject; // what we are moving
var here:Point=new Point(do.x,do.y);
if (Point.distance(here,nextPoint)<velocity) {
// this means we are too close to interpolate, just place
do.x=nextPoint.x;
do.y=nextPoint.y;
position++;
if (position>=sections.length) {
// movement finished
// TODO make your final animation
do.removeEventListener(Event.ENTER_FRAME,moveABit); // stop moving
}
} else {
// interpolate movement
var angle:Number=Math.atan2(nextPoint.y-here.y,nextPoint.x-here.x);
do.x+=Math.cos(angle)*velocity;
do.y+=Math.sin(angle)*velocity;
// if the object will move to wrong direction, fix this code!
}
}
If you want to see if your 'shape' is complete:
You can create a vector of boolean variables(every variable indicate a node of your wires- you set the variable to true when is connected), and you need a function to check if all the vars from vector are true (with loop), that means your shape is complete. Hope this idea helps!
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
}
}
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);
I have 27 MovieClips in my Library which are all images. At the moment they are positioned on the stage as instances of their parent and then made to function in the first frame of my actions layer. I want to recreate this layout solely in code so there is nothing on the stage. How do I do this?
Thanks in advance.
Sam
Right click on a movieclip in the library, then go to Properties.
Tick "Export for ActionScript", then check the name where it says "Class". Hit OK.
Let's say this name was "Symbol1".
Then type this script:
var symbol1:MovieClip = new Symbol1();
addChild(symbol1);
var symbol1 means you created a variable, MovieClip is the type. This MovieClip variable is a "new " Symbol1 (this was the name in the library, Properties, Class.
Then add this to the stage:
addChild(symbol1)
If you want to position it on the stage, set the coordinates of the variable:
symbol1.x = 10;
symbol1.y = 10;
puts it to (10, 10).
Depending on how many objects you have you can type this code for each one of them (don't forget to Export them for actionscript in Library->Properties).
If you have tons of movieclips and you don't want to type evertyhing, but would rather write some dynamic code, give us a hint on your library structure and how you named your objects.
Hope this helps.