as3 totalFrames return 1 - actionscript-3

I am trying to load one animation.
I want to know the end of the animation and avoid it in a loop.
var myLoader:Loader = new Loader( );
var myLoaderReq:URLRequest = new URLRequest("anim.swf");
myLoader.load(myLoaderReq);
addEventListener(Event.ENTER_FRAME , enterFrameListener );
addChild(myLoader);
myLoader.x = 10;
myLoader.y = 20;
function enterFrameListener(event:Event):void
{
var mc:MovieClip = event.currentTarget as MovieClip;
trace(mc.currentFrame);
if( mc.currentFrame == mc.totalFrames )
{
mc.removeEventListener(Event.ENTER_FRAME , enterFrameListener );
trace("end of movie reached");
}
}
for test i uses swf downloaded from http://www.leconcombre.com/movies/movies1us.html
any ideas how to do that?
thnx

The currentTarget you are using is refer to
addEventListener(Event.ENTER_FRAME , enterFrameListener );
Which should be your MainTimeline, if you add one more frame then should be return 2 of totalFrames.
As BadFeelingAboutThis saids, just replace:
var mc:MovieClip = event.currentTarget as MovieClip;
by
var mc:MovieClip = myLoader.content as MovieClip;
You also have to confirm you are attempting to manipulate the MainTimeline of anim.swf.

Related

action script 3.0 .I had tried a program that should move the dragger as well as the content movieclip,

In this code i need a progressbar to progress through while the imported .swf file is playing, meanwhile i should able to drag the dragger in the progress bar (i.e the rate of movement of dragger should sync with rate of .swf file playing). I got Argument error: #2109 Frame label 459.99 not found in scene1.
var loader:Loader = new Loader();
playBtn.visible = true;
pauseBtn.visible = false;
btn_00.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_01.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_02.addEventListener(MouseEvent.CLICK, fileLoaded);
function fileLoaded(evt:MouseEvent):void
{
var fileName:String = evt.currentTarget.name;
var fileNumber:String = fileName.split("_")[1];
var urlPath:String = "assets/file_" + fileNumber + ".swf";
loader.load(new URLRequest(urlPath));
addChild(loader);
}
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
function swfLoaded(event:Event):void
{
addEventListener(Event.ENTER_FRAME, trackPlayback);
}
function trackPlayback(event:Event):void
{
var perPlayed:Number = MovieClip(loader.content).currentFrame / MovieClip(loader.content).totalFrames;
progressbar.drag.x = (progressbar.bar.width - progressbar.drag.width) * perPlayed;
}
progressbar.drag.buttonMode = true;
var dragClicked:Boolean = false;
var xpos:Number = progressbar.bar.x * progressbar.drag.width;
progressbar.drag.addEventListener(MouseEvent.MOUSE_DOWN,dragMouseDown);
function dragMouseDown(evt:MouseEvent):void
{
trace(" inside mouse down ");
dragClicked = true;
progressbar.drag.startDrag(false,new Rectangle(xpos,0,progressbar.width-progressbar.drag.width,0));
}
progressbar.drag.addEventListener(MouseEvent.MOUSE_UP,dragMouseUp);
function dragMouseUp(evt:MouseEvent):void
{
dragClicked = false;
progressbar.drag.stopDrag();
var cnt:Number = (progressbar.drag.x/(progressbar.width-progressbar.drag.width))*MovieClip(loader.content).totalFrames;
MovieClip(loader.content).gotoAndPlay(cnt);
}
Pls solve my issue.
Thanks in advance.
To access a specific frame, you need to provide an integer value where at the moment you are using a floating-point value.
A simple fix would be to cast the Number to an int:
MovieClip(loader.content).gotoAndPlay(int(cnt));

Draw Rentangle inside a MovieClip ActionScript 3

I have a map consisting of MovieClip cities inside it and I have a click function in top layer of map MovieClip. I try to do that if I click a city, a rectangle will be drawn. Here is code:
function rpress(a)
{
trace( "trying" );
var b:MovieClip = new MovieClip();
b.graphics.beginFill(0xFF0000);
b.graphics.drawRect(0,0,100,80);
b.graphics.endFill();
b.x = 150;
b.y = 150;
addChild( b );
trace("done")
}
Trace commands are executed but no rectangle is drawn. I tried MovieClip( root ).addChild..., stage.addChild..., MovieClip( parent ).addChild... and others...
Do you have any idea? Thank you!
FULL CODE:
Double Click map MovieClip->84 layers welcome us->Chose the layer named "Action Layer" ACTIONS-FRAME:
function rbtxt(a)
{
var _loc2 = a;
var _loc3 = this;
balon._visible = true;
arbtxt = ilad.split(",");
balon.txt.text = arbtxt[_loc2];
_loc3["x" + _loc2].play();
balon._x = _loc3["x" + _loc2]._x;
balon._y = _loc3["x" + _loc2]._y - _loc3["x" + _loc2]._height / 2 + 5;
}
//End of the function
function rbalon(a)
{
balon._visible = false;
this["x" + a].gotoAndStop(1);
}
//End of the function
function rpress(a)
{
trace( "trying" );
var b:MovieClip = new MovieClip();
b.graphics.beginFill(0xFF0000);
b.graphics.drawRect(0,0,100,80);
b.graphics.endFill();
b.x = 150;
b.y = 150;
addChild(b );
trace("done")
}
ilad = "CITY NAMES....."
ilurl = "CITY URLS....."
You are not showing enough code, so we can only guess. By looking at the info you supplied you could try:
this["x" + a].addChild( b );
or
balon.addChild( b );
But this is only guessing...
Hi try to add the movie clip to the stage and then execute the drawing camamnd like this
var b:MovieClip = new MovieClip();
addChild(b );
b.graphics.beginFill(0xFF0000);
b.graphics.drawRect(0,0,100,80);
b.graphics.endFill();
b.x = 150;
b.y = 150;
Sprite has access to graphics, try and only use Movieclip for complex objects such as SWFs loaded in and Flash library assets with timeline based animation and variables etc
function drawRect()
{
trace( "trying" );
//var b:MovieClip = new MovieClip();
var b:Sprite = new Sprite();
b.graphics.beginFill(0xFF0000);
b.graphics.drawRect(0,0,100,80);
b.graphics.endFill();
b.x = 150;
b.y = 150;
addChild( b );
trace("done")
}

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();
}

ActionScript 3.0 Getting the index [CLICK event] of a loaded movie Clip image

I am loading a set of thumbnail images from an array [hard coded] into a movieclip symbol on the stage. I have two arrays with the thumbnail and the full size image having the same index number. In many examples, "event.currentTarget.contentLoaderInfo.url" returns the full path to the image selected. i just want the index number.
Adobe does not make is easy to figure out what other properties are available to me from the contentLoaderInfo. Is 'SelectedIndex' or something like that available?
Where does an inspiring AS programmer find the contentLoaderInfo properties and or methods available? Is url the only thing that us usable here?
Is there a better approach?
Thanks in advance.
Edit:
var thumbnails:Array = ["tn_2010OpenHouse_00.jpg","tn_2010OpenHouse_01.jpg"];
var images:Array = ["2010OpenHouse_00.jpg","2010OpenHouse_01.jpg"];
var thumbX:Number = 10;
var thumbY:Number = 623;
var loader:Loader = new Loader();
loader.load(new URLRequest("images/" + images[0]));
addChild(loader);
loadThumbs();
function loadThumbs():void
{
var thumbLoader:Loader;
var container:Sprite = new Sprite();
container.width = 100;
addChild(container);
container.buttonMode = true;
for (var i:uint = 0; i < thumbnails.length; i++)
{
thumbLoader = new Loader();
thumbLoader.load(new URLRequest("images/" + thumbnails[i]));
thumbLoader.x = thumbX;
thumbLoader.y = thumbY;
thumbX += 100;
container.addChild(thumbLoader);
thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
container.width += 100;
addChild(thumbLoader);
}
stop();
}
function thumbClicked(ev:MouseEvent):void
{
//weltraumpirat's example
var index:int = thumbnails.indexOf ( ev.target.contentLoaderInfo.url );
trace("Index= "+ index);
//trying a different approach as well
index = thumbnails.indexOf ( ev.currentTarget.contentLoaderInfo.url );
trace("Index= "+ index);
loader.load(new URLRequest("images/" + images[index]));
}
Output:
Index= -1
Index= -1
Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.
The contentLoaderInfo property returns a LoaderInfo class. You can view its properties here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/LoaderInfo.html
You can use array.indexOf() to return an object's index. Since I don't know the rest of your code, here's an approximate example:
function thumbClicked (ev:MouseEvent) : void {
var index:int = thumbnails.indexOf ( ev.target.contentLoaderInfo.url );
loader.load ( new URLRequest (fullSizeImages[index]) ) ;
}
Edit:
Since I didn't know the exact code you were using, I assumed you store the entire path to the picture in your array. In your code, you prepend "images/", so the code should be:
function thumbClicked (ev:MouseEvent) : void {
var index:int = thumbnails.indexOf ( ev.target.contentLoaderInfo.url.substring (7));
// 7 is the length of "images/", so substring returns only the filename part.
loader.load ( new URLRequest (fullSizeImages[index]) ) ;
}

Controlling as2 swf playback in as3

I am embedding a swf built in flash 8 into an as3 project. When I call stop() or gotoAndStop(0); on the MovieClip that represents an instance of the embedded swf it stops for a sec and then continues. Trying to call removeChild on the mc removes it from the display but the audio in the swf keeps playing. The swf, in this case must be embedded, I cannot use loader. Any ideas
The code:
[Embed (source = "t1.swf")]
private var t1:Class;
private var mc:MovieClip;
public function iphoneTest()
{
var tf:TextField = new TextField();
tf.x = 10;
tf.y = 100;
tf.width = 100;
tf.height = 50;
tf.text = "Hello worl";
mc = new t1();
var button:CustomSimpleButton = new CustomSimpleButton();
button.width = 50;
button.height = 50;
button.x = 10;
button.y = 150;
button.addEventListener(MouseEvent.CLICK, onClick);
this.addChild(mc);
this.addChild(tf);
this.addChild(button);
}
private function onClick(e:MouseEvent):void {
mc.stop();
this.removeChild(mc);
}
did you try mc = null;?
also since you know it's an as2 swf, should probably use avm1movie instead of MovieClip
At very worst you can just kill all sounds in the SWF...
Make sure you import the sound mixer class then kill the sound..
import flash.media.SoundMixer;
SoundMixer.stopAll();
If your SWF has any hierarchy, you'll need to recurse through it to stop all movie clips.
private function stopAll(do:DisplayObject):void
{
var clip:MovieClip = do as MovieClip;
if (clip != null)
clip.stop();
var container:DisplayObjectContainer = do as DisplayObjectContainer;
if (container != null)
{
for (var i:int = 0; i < container.numChildren; ++i)
{
stopAll(container.getChildAt(i));
}
}
}