Bitmap as button? - actionscript-3

How to set a bitmap as a button so that i can apply button mode and mouse-event stuff on it, without adding the bitmap to a Movie Clip?
var bmpFull=new Bitmap(event.currentTarget.content.bitmapData);
bmpFull.smoothing=true;
bmpFull.name="photo";
bmpFull.alpha=0;
//fullMC.buttonMode=true;
fullMC.addChild(bmpFull);

Unfortunately, Bitmap objects do not extend from the InteractiveObject class - that is, they don't have (and cannot easily get) the ability to receive mouse events.
As pointed out by antpaw and Jeremy White in the previous answer, the simplest container class that does receive mouse events is the Sprite class. Therefore, if you wanted to have a Bitmap receive mouse events, and not use a MovieClip, you could use a Sprite:
var bmpFull:Bitmap = new Bitmap(event.currentTarget.content.bitmapData);
bmpFull.smoothing = true;
bmpFull.name = "photo";
bmpFull.alpha = 0;
var bmpContainer:Sprite = new Sprite(); // can receive mouse events, for example:
bmpContainer.addEventListener(MouseEvent.CLICK, clickHandler);
bmpContainer.buttonMode = true; // this just makes it show the hand cursor, and is not necessary for the mouse events to work
bmpContainer.addChild(bmpFull);
In fact, I would recommend using a Sprite, as they're simpler objects than MovieClips and thus do not require as much memory.
Now, if you wanted to make a Bitmap dispatch mouse events without using any sort of container clip, you'd probably need to write your own extension of the Bitmap class that had it's own event manager. That would be far, far more complicated. I highly recommend just using a Sprite as a container.

buttonMode is a property of Sprite
inheritance of a movie clip goes like this
MovieClip >> Sprite >> DisplayObjectContainer >> InteractiveObject >> DisplayObject >> EventDispatcher >> Object
Bitmap >> DisplayObject >> EventDispatcher >> Object

Related

Accessing color of sprite in array - Flash

I have an array of rectangular sprites that are on the stage, each with a different ColorTransform value. I want to be able to click on any one of these rectangles and access the ColorTransform value of this object. What is the best method of accessing whichever rectangle was clicked on?
You can add the same click handler to each rectangle sprite and use event.currentTarget to handle which sprite was clicked: Sprite(event.currentTarget).transform.colorTransform.
Or, if you put all the sprites in the same container, you can add a click handler to the container and use event.target to know which sprite in the container was clicked: Sprite(event.target).transform.colorTransform.

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
}
}

AS3...instantiating movieclips from library and also creating them dynamically...is stop() needed?

In the past, we've put a stop() action in the timeline of movieclip symbols so that the timeline would not play and we would control all animations via code.
We've also done that to the main timeline as well.
Is this still needed for performance reasons? Is this needed for dynamically created movieclips?
I know that the Sprite class should be used if there is no timeline associated with it.
You don't need to do this. When you create your instance, if your movieClip has more than one frame simply call stop on that instance:
var ball:MovieClip = new Ball();
ball.stop();
addChild(ball);

Switching flash scenes within a MovieClip

How do I switch scenes within a flash MovieClip object with Flash CS5 and ActionScript 3?
Hi Dani,
First of all, coding within MovieClip objects in AS3 is not recommended AND using scenes are not recommended.
Why?
Coding within MovieClip objects can be tempting if you are a beginner and this is alright.But if you are serious about the reusability of your assets and your code, you may want to dissociate your visual from your logic.I'm suggesting you write your logicFrom the main timelineFrom external classes (and using OOP)This is great!
Scenes are bad because of the timeline and code issues.If some of your code is in a scene, it may be not accessible to the other scenes.
Enough talking, here's the help you need.
How do I switch scenes within a Flash MovieClip object
This code is intended to be in a MovieClip frame
// === Let's put the stage in a variable (cleaner) ===
var main:MovieClip = this.parent as MovieClip;
// this.parent will return the DisplayObject of parent the current clip.
// You need to cast [... as MovieClip] to not cause errors because Flash
// thinks it is only a DisplayObject
// === Here's the interresting part ===
main.gotoAndPlay(0, "Scene 2");
// We tell the main timeline to go to frame 0 in the "Scene 2"
// Be cautious, it must be spelled exactly as displayed in Flash (IDE)
Don't forget: Deeper is your clip (Embeded multiple times in a clip), more "parent" will you need.
var main:MovieClip = this.parent.parent as MovieClip;
// If your object is inside a MovieClip who is itself in a MovieClip
// Tip: How much time you need to push the Back button to go to the timeline
// is the number of parents you need to write.
Hoping this help. If you have any questions, just comment this answer!

Adding a object to a bitMapData

As of right now. I have 3 objects. One BitMap that acts as my canvas. And 2 bitmapDatas. One is my buffer and another is my tiles. I am creating a tiling effect for a game. I would like to take my tile:BitMapData, and turn it into a custom object. reason being is I want each tile to be interactive. So I can click on each one. Is it possible to turn my bitMapData that represents a tile, into a custom object that has properties and methods. Sort of like a movie clip. and draw it into my buffer ?? Could I create a new class that extends bitMapData ?? Or would I have to get rid of the buffer and draw the tile objects directly into the BitMap ??
In other words, what is the best way to put Sprite or a tile into a BitMapData object or even a Bitmap.
First of all, BitmapData and Bitmap are not interchangeable. They are two very different things. The BitmapData class contains bitmap pixel data, and allows you to manipulate that pixel data, e.g. draw to it, change the color of particular pixels, et c. There is no way of displaying a BitmapData directly, i.e. by adding it to the display list.
The Bitmap class, on the other hand, is a DisplayObject, like MovieClips and Sprites, which you can add to the display list. It's single purpose is to render a BitmapData in the display list. In fact, it is not even interactive, so you cannot detect clicks directly on a Bitmap instance, for example.
On to your question: If you have a bitmap data that contains a tile sprite, and you want to draw that tile in another bitmapdata, you can use the BitmapData.draw() method, or the BitmapData.copyPixels() method. The latter is one of the fastest methods you can use on any BitmapData, so I would highly recommend it.
Depending on your particular application, it might not be beneficial to draw everything in a bitmap at all. It sounds as if you want to be able to detect click events on all the tiles, which makes me think that you would probably benefit from having them be separate DisplayObjects, e.g. Sprites.
If you want to, you can create a Tile class that extends Sprite, and draws a BitmapData using a bitmap fill. That way, you can have any properties you want, and also detect mouse events on the tile instances.
package
{
/* ... imports ... */
public class Tile extends Sprite
{
private var _solid : Boolean;
public function Tile(bmp : BitmapData, solid : Boolean)
{
this.graphics.beginBitmapFill(bmp, null, false, true);
this.graphics.drawRect(0, 0, bmp.width, bmp.height);
_solid = solid;
}
/**
* Sample custom property. Could be used to define whether a tile
* is solid, e.g. the player cannot pass it.
*/
public function get isSolid() : Boolean
{
return _solid;
}
}
}
This class could simply be instantiated for every tile in your game, passing in the bitmap data that should be drawn in the tile. You could also listen for events on such a tile instance.
var tile : Tile;
tile = new Tile(myBitmapData, false);
tile.x = 200;
tile.y = 200;
tile.addEventListener(MouseEvent.CLICK, handleTileClick);
addChild(tile);
This way, you don't have to use the Bitmap class at all to render the tiles. They can be added directly to the display list.
I figure a better solution for when you want to select certain parts of the tile without effecting the position of the sprite itself
theMatrix.translate(30,0);
this.graphics.beginBitmapFill(tileImage,theMatrix);
//this.graphics.drawRect(0, 0,tWidth ,tHeight );
this.graphics.endFill();
Your right, leave drawRect at 0, 0. using Matrix.Translate, allows you to move the positioning of what part of the tile you want, without affecting the sprite position itself.