AS3 - Apply BitmapData addChild to multiple MovieClips - actionscript-3

I need to add "_myThumb" to 4 container MovieClips. The problem is that it's only working for 1 MovieClip. What do I need to change?
var _myThumb:Bitmap;
var _myThumbData:BitmapData;
function createThumbs()
{
_myThumbData = new BitmapData(photodefault.width,photodefault.height,false,0xffffff);
_myThumb = new Bitmap(_myThumbData);
_myThumb.smoothing = true;
_myThumb.scaleX = _myThumb.scaleY = 0.2;
// Add to t1-t4 container
photothumbs.t1.addChild(_myThumb);
photothumbs.t2.addChild(_myThumb);
photothumbs.t3.addChild(_myThumb);
photothumbs.t4.addChild(_myThumb);
}
createThumbs();
function createThumbnail()
{
_myThumbData.draw(photodefault);
}
Thanks.
Uli

You need to create separate Bitmap objects for each thumb, but you can use the same source Bitmapdata for that. This is an example using a utility function to create the bitmap object:
function createThumbs()
{
_myThumbData = new BitmapData(photodefault.width,photodefault.height,false,0xffffff);
// Add to t1-t4 container
photothumbs.t1.addChild(createBitmap(_myThumbData));
photothumbs.t2.addChild(createBitmap(_myThumbData));
photothumbs.t3.addChild(createBitmap(_myThumbData));
photothumbs.t4.addChild(createBitmap(_myThumbData));
}
function createBitmap(bmd:BitmapData):Bitmap
{
var bitmap:Bitmap = new Bitmap(bmd);
bitmap.smoothing = true;
bitmap.scaleX = bitmap.scaleY = 0.2;
return bitmap;
}

Related

use 1 object multiple times in as3?

I'm trying to make something like bookmarks, I have 1 note on the stage and when the user clicks it, it starts to drag and the users drops it where they want. the problem is I want these notes to be dragged multiple times.. here is my code:
import flash.events.MouseEvent;
//notess is the instance name of the movie clip on the stage
notess.inputText.visible = false;
//delet is a delete button inside the movie clip,
notess.delet.visible = false;
//the class of the object i want to drag
var note:notes = new notes ;
notess.addEventListener(MouseEvent.CLICK , newNote);
function newNote(e:MouseEvent):void
{
for (var i:Number = 1; i<10; i++)
{
addChild(note);
//inpuText is a text field in notess movie clip
note.inputText.visible = false;
note.x = mouseX;
note.y = mouseY;
note.addEventListener( MouseEvent.MOUSE_DOWN , drag);
note.addEventListener( MouseEvent.MOUSE_UP , drop);
note.delet.addEventListener( MouseEvent.CLICK , delet);
}
}
function drag(e:MouseEvent):void
{
note.startDrag();
}
function drop(e:MouseEvent):void
{
e.currentTarget.stopDrag();
note.inputText.visible = true;
note.delet.visible = true;
}
function delet(e:MouseEvent):void
{
removeChild(note);
}
any help will be appreciated.
You need to create a new instance of your note class when you drop, copy the location and other variables from the note you were dragging, add your new note to the stage, and return the dragging note to its original position.
Something like:
function drop($e:MouseEvent):void
{
$e.currentTarget.stopDrag();
dropNote($e.currentTarget as Note);
}
var newNote:Note;
function dropNote($note:Note):void
{
newNote = new Note();
// Copy vars:
newNote.x = $note.x;
newNote.y = $note.y;
// etc.
// restore original note.
// You will need to store its original position before you begin dragging:
$note.x = $note.originalX;
$note.y = $note.orgiinalY;
// etc.
// Finally, add your new note to the stage:
addChild(newNote);
}
... this is pseudo-code really, since I don't know if you need to add the new note to a list, or link it to its original note. If you Google ActionScript Drag Drop Duplicate, you will find quite a few more examples.
I think you are not target the drag object in drag function and problem in object instantiation
for (var i:Number = 1; i<numberOfNodes; i++) {
note = new note();
addChild(note);
...
....
}
function drag(e:MouseEvent):void{
(e.target).startDrag();
}
If you are dragging around multiple types of objects (eg. Notes and Images), you could do something like this, rather than hard coding the type of object to be instantiated.
function drop(e:MouseEvent):void{
// Get a reference to the class of the dragged object
var className:String = flash.utils.getQualifiedClassName(e.currentTarget);
var TheClass:Class = flash.utils.getDefinitionByName(className) as Class;
var scope:DisplayObjectContainer = this; // The Drop Target
// Convert the position of the dragged clip to local coordinates
var position:Point = scope.globalToLocal( DisplayObject(e.currentTarget).localToGlobal() );
// Create a new instance of the dragged object
var instance:DisplayObject = new TheClass();
instance.x = position.x;
instance.y = position.y;
scope.addChild(instance);
}

eventlistener doesn't execute its function

I need to put some thumbnails on the stage. they have different width and I need to get the width after They are loaded.I used the listener to find the width but the function which listener should call doesnt run.
why my code doesn't enter to the function of loadThumbs?
function makeScroller():void
{
for (var item:uint = 0; item < 7; item++ )
{
var thisOne:MovieClip = new MovieClip();
var blackBox:Sprite = new Sprite();
var thisThumb:Sprite = new Sprite();
var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest(thumbList[item]));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadThumbs);
function loadThumbs (e:Event):void
{
blackBox.graphics.beginFill(0xFFFFFF);
blackBox.graphics.drawRect(-1,-1,imageLoader.width,imageLoader.height);
thisOne.addChild(blackBox);
thisOne.blackBox = blackBox;
thisOne.x = tsX;
thisThumb.addChild(imageLoader);
thisOne.addChild(thisThumb);
tsX += imageLoader.width;
scroller.addChild(thisOne);
}
}
}
I have just tried a more simplified version of your code without any issues, maybe try this:
NOTE: Best practice is to add the COMPLETE listener BEFORE you call load also...
function makeScroller():void
{
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedHandler);
imageLoader.load(new URLRequest("http://images.imagecomics.com/c/2012/IMG120350.jpg"));
function loadedHandler(e:Event):void
{
trace("loaded");
}
}
makeScroller();

Actionscript 3: mouse_over & mouse_out causing Error #1009?

When I mouse over the buttonSprite, it adds displaySprite to the stage, and when I mouse out buttonSprite, the displaySprite is removed.
My problem is that when I swiftly mouse over and out the buttonSprite several times, the displaySprite is not be removed and an error message(Error #1009) is displayed. Even I type "displaySprite = null", it's still not working. Any suggestions? Thanks
var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OVER, overSprite);
var displaySprite:Sprite;
function overSprite(e:MouseEvent):void{
displaySprite = new Sprite();
addChild(displaySprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OUT, outSprite);
}
function outSprite(e:MouseEvent):void{
removeChild(displaySprite);
displaySprite = null;
}
There is no guarantee that the events will fire in order.
In your case you do not have to instantiate the displaySprite multiple times.
Just don't null it out and the object will be there.
var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OVER, overSprite);
//you only need to create it once.
var displaySprite:Sprite = new Sprite();
function overSprite(e:MouseEvent):void{
addChild(displaySprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OUT, outSprite);
}
function outSprite(e:MouseEvent):void{
removeChild(displaySprite);
}
How about this?
var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OVER, overSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OUT, outSprite);
var displaySprite:Sprite;
addChild(displaySprite);
displaySprite.visible=false;
function overSprite(e:MouseEvent):void
{
displaySprite.visible = true;
}
function outSprite(e:MouseEvent):void
{
displaySprite.visible = false;
}
The problem is MouseEvent.MOUSE_OVER is dispatched multiple times when you mouse over a display object. What you should try is MouseEvent.ROLL_OVER and MouseEvent.ROLL_OUT those two events are dispatched once.
Something along the lines of :
var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.ROLL_OVER, overSprite);
buttonSprite.addEventListener(MouseEvent.ROLL_OUT, outSprite);
var displaySprite:Sprite;
function overSprite(e:MouseEvent):void
{
if(!displaySprite)
{
displaySprite = new Sprite();
addChild(displaySprite);
}
}
function outSprite(e:MouseEvent):void{
if(displaySprite)
{
removeChild(displaySprite);
displaySprite = null;
}
}

as3 moving image from one mc to another

With the code below I created some imgMcA and some imgMcB then I loaded images into imgMcA ones. ImgMcBs have no image at that moment. So if one of imgMcA is clicked the image should be transferred to one of the empty imgMcBs (may be randomly) and if imgmcB is clicked later the image should move back to its imgMcA back. I could not find out how I can accomplish this.
Thanks in advance
function imageList(mcname, img, index){
var imgMcA:MovieClip=new MovieClip();
imgMcA.graphics.beginFill(0x000000);
imgMcA.graphics.drawRect(0,0,imgWidth,imgHeight);
imgMcA.graphics.endFill();
imgMcA.name=lemma;
imgMcA.addEventListener(MouseEvent.CLICK, moveImage);
var imgMcB:MovieClip=new MovieClip();
imgMcB.graphics.beginFill(0x000000);
imgMcB.graphics.drawRect(0,0,imgWidth,imgHeight);
imgMcB.graphics.endFill();
imgMcB.name=index;
addChild(imgMcB);
var imgLoader:Loader = new Loader();
imgLoader.load(new URLRequest(img));
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, changeProperties);
imgLoader.mouseEnabled=false;
imgMcA.addChild(imgLoader);
}
function moveImage(evnt:MouseEvent){
}
You could linked mcA & mcB by adding them to the same parent.
function createBlock():void
{
var imgLoader:Loader = new Loader();
//add the loading code here...
var mcA:Sprite = new Sprite();
var mcB:Sprite = new Sprite();
// add Click event listeners for both Mcs here...
var parent:Sprite = new Sprite();
//add Children
parent.addChild(mcA);
parent.addChild(mcB);
addChild(parent);
}
function mouseClickHandler(event:MouseEvent)
{
var dispatcher:Sprite = event.currentTarget as Sprite;
//if you added a Loader to it
if( dispatcher.numChildren > 0)
{
//retrieve the image
var img:Loader = dispatcher.getChildAt(0);
//identify the parent
var parent:Sprite = dispatcher.parent;
var index:int = parent.getChildIndex(dispatcher);
//identify the receiving MC
//of course this only works with two children!!!
if(index > 0)
var receiver:Sprite = parent.getChildAt(0) as Sprite;
else
receiver = parent.getChildAt(1) as Sprite;
//add the image to the other MC
receiver.addChild(img);
}
}
The rest is not too complicated to achieve. You will need to use a Boolean and add a TextField. If the TextField contains text, set the Boolean to true.
It may be worth looking at Classes or you could use an Object as a container for the MovieClips, the TextField and the Boolean, although a Class will give you more flexibility...
With a Class, you wouldn't have to iterate in order to find what does what. Your Click listener would look something like this:
private function mouseClickHandler(event:MouseEvent)
{
receiver.addChild( image );
if( hasText)
imageReturn();
}

AS3 clone MovieClip

The below is my code for trying to clone the MovieClip and it doesn't work.
We should see two cirles if the codes is working correctly.
/*The original MovieClip*/
var circle:MovieClip = new MovieClip();
circle.graphics.beginFill(0xAA0022);
circle.graphics.drawCircle(40, 40, 40);
circle.x=10
addChild(circle);
/*CLONE the MovieClip - IT DOES'T WORK FROM HERE DOWN*/
var cloneCirle:MovieClip = new MovieClip();
cloneCirle=circle
cloneCirle.x=60
addChild(cloneCirle);
When you do cloneCircle=circle, it's not copying or cloning anything. It's just saying that the variable cloneCircle is another name for your original circle MovieClip. What you need to do is use the Graphics.copyFrom() method.
Try it:
var cloneCircle:MovieClip = new MovieClip();
cloneCircle.graphics.copyFrom(circle.graphics);
cloneCircle.x = 60;
addChild(cloneCircle);
This is for creating a duplicate of a stage object that exists in the FLA library at compile time
The object must have a 'Export for Actionscript ticked in it's properties panel and a valid class name in the 'Class' box
If the symbol only has a single frame just add another so it registers as MovieClip() rather than Sprite()
private function cloneObject(source:DisplayObject):void
{
var objectClass:Class = Object(source).constructor;
var instance:MovieClip = new objectClass() as MovieClip;
instance.transform = source.transform;
instance.filters = source.filters;
instance.cacheAsBitmap = source.cacheAsBitmap;
instance.opaqueBackground = source.opaqueBackground;
source.parent.addChild(instance);
instance.x += 20; // just to show the duplicate exists!
}
http://snipplr.com/view/44734/
Adapted from here:
function copyClip( clip:MovieClip )
{
var sourceClass:Class = Object(clip).constructor;
var duplicate:MovieClip = new sourceClass();
return duplicate;
}