Simple way to change a MovieClip with another MovieClip - actionscript-3

I converted some images to movieclips in animate cc, images are text exported to .png and they are different in width same in height. Both are in library and stage but in different position and in same Frame, I need to swap/replace each other in different cases with code.
I used blow code for this purpose but nothing happened.
Code:
var mc:MovieClip = new MovieClip();
addChild(MC_TEXT21);
mc.x =642;
mc.x =495;
trace("mc: "+mc);
trace("MC_TEXT21: "+MC_TEXT21);
trace(mc.stage);
trace(mc.root);
Console:
mc: [object MovieClip]
MC_TEXT21: [object MovieClip]
null
null

Right click on symbols in Library and choose Properties
In Symbol Properties select "Export for ActionScripts" and "Export in frame 1"
Hit OK (if a warning show, again hit OK)
Use blow code for Add/Remove MovieClips:
var mc:MC_TEXT21 = new MC_TEXT21();
mc.x =642;
mc.y =495;
addChild(mc); // This code add Object to Stage
removeChild(mc); // This code remove Object from Stage

Related

Actionscript 3 how to duplicate a symbol onto the next frame

Im a total beginner at using AS3. I want to know is how does one duplicate a symbol and make it appear in the next frame.
example: when the user clicks on the symbol in that frame, the same symbol will appear in the next frame. If it is not possible then how does one move that symbol to the next frame.
Thank You in advance for answering.
You should probably avoid to use timeline keyframe to achieve this kind of thing. However, if you really need to do it this way, here what I would do :
First, make sure the MovieClip you want to clone has ActionScript linkage.
Then:
my_mc.addEventListener(MouseEvent.CLICK,onClick)
function onClick(e:MouseEvent):void{
var m:MovieClip = MovieClip(e.currentTarget);
var c:Class = Object(m).constructor;
var clone:MovieClip = new c() as MovieClip;
gotoAndStop(2);
addChild(clone);
}
This will add the clone to the stage, so if you go back to frame 1, you'll see the clone. There's no way to add an object to a specific timeline frame. If you want to achieve such a thing, you have to target a container on frame 2 and add to clone to the container.
something like this :
function onClick(e:MouseEvent):void{
var m:MovieClip = MovieClip(e.currentTarget);
var c:Class = Object(m).constructor;
var clone:MovieClip = new c() as MovieClip;
gotoAndStop(2);
myContainerOnframe2.addChild(clone);
}

HitTest not working correctly when using Graphics.lineTo/curveTo

So I have a Movie Clip called hookLine thats added to the stage from my mainEngine class. This empty movieClip is connected to my fisherman Movie Clip and curves to my playerHook Movie Clip. Its added and connected to the stage like so:
In My mainEngine function loop:
playerHookLine();
Then the Function:
private function playerHookLine():void
{
//Add hook line to fisherman and playerhook
hookLine.graphics.clear();
hookLine.graphics.lineStyle(1);
hookLine.graphics.moveTo(fisherman.x, fisherman.y);
hookLine.graphics.curveTo(playerHook.x, playerHook.y, mouseX, mouseY);
}
Now the problem I am having is whenever I try to hitTest the hookLine with a Move Clip called currentShark the hitTest works and I get a trace, but its not ACCURATE at all when I curve my hook line to the sides and the currentShark comes on stage it automatically hitTests and gives me the trace. So basically the shark doesnt even have to come in contact with the actual Line Graphic. Right when the shark is added to the stage it just registers.
Does anyone have any idea why this is?
Here is how the hitTest function is:
private function checkPlayerHitShark():void
{
//Loop through all sharks
for (var i:int = 0; i < aSharkArray.length; i++)
{
//Get current Shark in i loop
var currentShark:mcShark = aSharkArray[i];
//Check if shark is hittest with Hook
if (currentShark.hitTestObject(playerHook) || currentShark.hitTestObject(hookLine))
{
trace("Hook Hit Shark");
trace("hit LINE");
removePlayerLive();
//Destroy player
playerHook.destroyPlayerHook();
hookLine.destroyHookLine();
//Remove shark from array
aSharkArray.splice(i, 1);
//Add new Hook to stage
stage.addChild(playerHook);
stage.addChild(hookLine);
}
}
}
More than likely the bounding boxes of your shark and fishing line are colliding. As your curved fishing line moves to the left or right, your bounding box will be the same as the width and height of the fishing line itself. Open your project and publish as SWF, then open the SWF in Flash player and press Control+E or click on View at the top of the window and select "Show Redraw Regions". You should see the bounding boxes in red as they are redrawn to the stage.
What you are looking for is pixel level hit detection on the bitmaps of your shark and fishing line. BitmapData has a method called hitTest which will require a few parameters.
You are going to find excellent help for pixel level hit detection from an article written by Mike Chambers at the link here:
http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/
The documentation for BitmapData.hitTest can be found here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html
Just look for the list of public methods.

AS3 Getting to Control Stage

I am building a Flash Game, I am doing this:
MainStage, with 11 frames
Mainstage Loads > Lobby.swf (1 frame)
Lobby Loads > Arrows.swf (1 frame)
I just want to control the main Stage from Arrows.swf , get the current frame, right arrow mainStage.current frame + 1, left arrow mainStage.current frame -1
I have tried the parent/child method
this.parent.parent
parent.parent
this.root
parent.root
every combination, I am also tracing and when I finally hit the mainStage it is saying cannot convert Stage to Display Object which I understand.
but how can I just get current frame of the "mainstage" needle and control it with gotoAndPlay or other such methods ?
I have solved it.
I didn't control the main stage what I did was, I reduced the main stage layer in the lobby layer when I load the left and right arrow I attach an event listener for a custom event.
In the arrows movie I have written a function to dispatch the event here are the lines of code I used
in the "main" stage of the movie you want to control:
//ball loader prep and load
var ballLoader:Loader = new Loader();
var ballRequest:URLRequest = new URLRequest("lobbyBalls.swf");
ballLoader.load(ballRequest);
ballLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, ballsLoaded);
function ballsLoaded(event:Event):void{
trace("ball Loader complete");
ballLoader.x = 50;
ballLoader.y = 200;
stage.addChild(ballLoader);
ballLoader.content.addEventListener("Previous", ctrlPrevFrame);
ballLoader.content.addEventListener("Next", ctrlNextFrame);
}
In the movie from which you want to control
import flash.events.Event;
leftArrow.addEventListener(MouseEvent.CLICK, prevEvent);
rightArrow.addEventListener(MouseEvent.CLICK, nextEvent);
function prevEvent(event:Event):void{
trace("prev");
dispatchEvent(new Event("Previous", true));
}
function nextEvent(event:Event):void{
trace("next");
dispatchEvent(new Event("Next", true));
}
Thanks anyways....

MovieClip doesn't dispatch mouse event after nesting MovieClip loaded from external SWF

Mostly, problem is described in the title... I tried to load an external SWF file that contains some named MovieClip instances (exporting and naming is done by Flash CS5 software) and to add some of externaly loaded (named) MovieClip-s in MovieClip object which is created in my code. Problem appears when i add MOUSE_CLICK listener to parent MovieClip. Simply, it does not dispatch event when i click on it at the stage...
private var loader:Loader;
public function Example(){
loader = new Loader();
var request:URLRequest = ... // URL to external SWF
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingCompleted)
loader.load(request);
}
private function loadingCompleted(event:Event):void{
var mc:MovieClip = loader.content as MovieClip;
var myMovie:MovieClip = new MovieClip();
myMovie.addChild(mc.getChildByName("object_name"));
myMovie.addEventListener(MouseEvent.CLICK, myMovieClicked);
addChild(myMovie); // myMovie (with nested mc) appears on the stage
}
private function myMovieClicked(evt:Event):void{
//never reached
}
EDIT: I didn't mention that i'm working in Flex using FlashBuilder 4.5 where i created ActionScript project. Code above is body of Example class, which is main SWF class.
EDIT AFTER ANSWER: myMovie.mouseChildren = false solves the problem. Earlier i tried to set mouseEnabled = true, and it didn't fix the problem. But i'm confused about event flow now... Even if child is target node, why mouse listener on parent MovieClip doesn't recieve event (in capture phase) when parent is still on event flow? Moreover, when i create another movie clip in my code (whit some simple shape inside) and add it to myMovie, everything works fine. What is so special when i obtain movie clip from externaly loaded SWF?
Have you tried doing myMovie.mouseEnabled = true and myMovie.mouseChildren = false ?

ActionScript BitmapData Built-in To Bitmaps?

i've used a Loader and URLRequest to download a .png from the internet and add it to my display list. since it's already a bitmap, does it have built in bitmap data already? or do i have to create the bitmap data myself?
also, why does the same trace statement return false in the mouseMoveHandler when it outputs true in the displayImage function?
var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest("http://somewebsite.com/image.png"));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayImage);
function displayImage(evt:Event):void
{
addChild(evt.target.content);
addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
trace(evt.target.content is Bitmap); //outputs 'true'
}
function mouseMoveHandler(evt:MouseEvent):void
{
trace(evt.target.content is Bitmap); //outputs 'false'
}
A quick search of the AS3 docs tells me that Bitmap has a bitmapData property.
You are getting different rusults in each trace because you are tracing different things. try just tracing the property rather that "is Bitmap", to see what is actually stored there.
Your first trace you are tracing an Event, your second a MouseEvent. Your displayImage function is a "Loader Complete handler", so target will be a LoaderInfo object. In a LoaderInfo object, target refers to "The loaded DisplayObject associated with this LoaderInfo object". But in a MouseEvent the target will be different. You will need to refer to the docs for each event to find out what the target will be.
Also, I think you will need to add the mouse move event listener to the stage, or it wont work e.g.
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);