Alright, so I'm making this little game in ActionScript using Flash Builder. The duck asset has an animation. I used duck.stop(); to stop the Movie Clip so the animation doesn't play.
However, when I click on the duck, I need to figure out a way to once against start the Movie Clip. Does anyone know a way I could go about doing this?
private function makeDucks(amount:uint):void
{
for(var j:int = 0; j < amount; j++){
var duck:Duck = new Duck();
addChild(duck);
duck.x = j * (duck.width + duck.width / 3);
// .stop stops the MovieClip
duck.stop();
}
}
duck.addEventListener(MouseEvent.CLICK, onDuckClicked);
private function onDuckClicked(e:MouseEvent):void
{
var duck:MovieClip = e.target as MovieClip;
if(duck)
duck.play();
}
Related
I have a complicated problem that I don't know where it comes from.
I have three movie clip in my first frame that each one call "homework" function.
Problem:
There is something wrong with the third one that if you click on it first it works correctly, but if you click on the other objects and then come back and try third one it doesn't work anymore.
I don't have this problem with the others MovieClip objects so how is this problem possible for third MC using the exact same function?
Here is the homeWork function:
function homeWork(a :int) :void
{
gotoAndStop(2);
cubeContainer.addChild(movieClip);
movieClip.x= 20;
movieClip.y= 20;
var j:int =0;
for( var i = a; i < (a+10); i++)
{
cubeContainer.addChild(photos[i]);
photos[i].width = 130;
photos[i].height = 130;
photos[i].x = j-80;
photos[i].y = stage.height-photos[i].height;
j += 90;
photos[i].doubleClickEnabled=true;
photos[i].addEventListener(MouseEvent.DOUBLE_CLICK ,covering);
dragAndDrop(photos[i]);
}
}
Hey everyone so I have a Movie Clip inside my platforms movie clip called mcTaco so platforms.mcTaco. I add the platforms and the player which I check for hitTest collisions to the stage dynamically using code. When I check for the player and platforms.mcTaco collison everything works fine. It's when I duplicate the mcTaco creating multiple instances of them inside the platforms movieclip that I am having the main issues.
I know I have to add them to an array and use a for loop to check for the hitTest of the multiple platforms.mcTaco instances but I don't know how to accomplish this. I am using an IDE Flash Develop for my code and here is what I have so far:
I add my Platforms to the stage like so:
//ADD PLATFORMS AND ENVIRONMENT
private function addPlatForms():void
{
//Add Obstacle Platforms
platforms = new mcPlatforms();
platforms.x = (stage.stageWidth / 2) - 80;
platforms.y = (stage.stageHeight / 2) + 165;
addChildAt(platforms, 1);
aPlatformArray.push(platforms);
//trace(aPlatformArray.length + " NPLATFORMS");
}
and my Player:
//Add Character
player = new mcPlayer();
player.x = (stage.stageWidth / 2) - 80;
player.y = (stage.stageHeight / 2) + 78;
addChildAt(player, 1);
and in my ENTER_FRAME Event Listener I am doing this to test the player and platforms.mcTaco hitTest like so:
private function tacoHitTestHandler():void
{
if (player.hitTestObject(platforms.mcTaco))
{
platforms.mcTaco.visible = false;
trace("HIT TACO");
}
}
I just need some help on figuring out how to go about doing this. I have my Taco Arrayprivate var aTacoArray:Array; instantiated but I just don't know how to push the platforms.mcTaco Movie clips into it to use the for loop to test it. Any help on how to go about doing this would be greatly appreciated. I just don't want to have to do the if (player.hitTestObject(platforms.mcTaco1) || player.hitTestObject(platforms.mcTaco2) etc...)
You need to loop children inside each of the platforms to get all tacos by their class or/and name.
private function tacoHitTestHandler():void
{
// Loop through the list of the platforms.
for (var i:int = 0; i < aPlatformArray.length; i++)
{
// Get a platform instance from the Array.
var aPlatform:mcPlatforms = aPlatformArray[i];
// Loop through the children of a platform.
for (var j:int = 0; j < aPlatform.numChildren; j++)
{
// Get a potential taco.
var aTaco:mcTaco = aPlatform.getChildAt(j) as mcTaco;
// Skip if it is not a taco.
if (!aTaco) continue;
// Ignore invisible objects.
if (!aTaco.visible) continue;
// Ignore wrong tacos by name.
if (aTaco.name != "mcTaco") continue;
// Hit test against taco.
if (player.hitTestObject(aTaco))
{
aTaco.visible = false;
trace("HIT TACO");
}
}
}
}
A few notes.
First, calling a class mcTaco and giving the same instance name mcTaco is asking for headache because there are moments where your code will not be sure, if you are referring a class of a declared instance.
Second, call your classes with uppercase head letter because it's nice. A nice scottish McTaco would suffice.
You push your tacos to the aTacoArray the same way you do it with platforms:
aTacoArray.push(myTacoMC);
Then you can loop trough them and test the collision on each:
private function tacoHitTestHandler():void
{
for(var i:int = 0; i < aTacoArray.length; i++)
{
// get taco from array
var currentTaco:MovieClip = aTacoArray[i];
if (player.hitTestObject(currentTaco))
{
currentTaco.visible = false;
trace("HIT TACO");
}
}
}
I would like to make my Flash AS3 based app more accessible with a keyboard navigation.
What's the best way to add to every MovieClip with a MouseEvent.CLICK the ability to get selected through the TAB and clicked/fired through ENTER?
Some basic example of my code:
nav.btna.addEventListener(MouseEvent.CLICK, openSection);
dialog.btnx.addEventListener(MouseEvent.CLICK, closeDialog);
function openSection(event:Event=null):void
{
trace("nav.btna")
}
function closeDialog(event:Event=null):void
{
trace("dialog.btnx")
}
I remember that there was a AS3 function that enabled that every MovieClip with a MouseEvent could be fire through ENTER if the MovieClip was selected with TAB. I can't remeber the function though.
I think the problem may be that you are attempting this with a MovieClip instead of a button (Button or SimpleButton).
I made a simple test by creating buttons instead of MovieClips in my library and this worked as expected:
// I have 4 buttons (button1, button2, etc) on the stage
for(var i:int = 1; i <= 4; i++)
{
var mc = getChildByName("button" + (i+1));
mc.tabIndex = i;
mc.addEventListener(MouseEvent.CLICK, onClicked);
}
function onClicked(e:MouseEvent):void
{
trace(e.currentTarget + " clicked");
}
stage.focus = stage;
I initially ran this test with MovieClip instances, and while they would show that the tab was working (a yellow border shows up), the MouseEvent.CLICK was never firing. Once I switched to actual buttons (SimpleButton in this case), it worked with both the Enter and Space keys.
EDIT:
To answer the question posed in the comments, this is a quick-and-dirty way to "convert" MovieClips to SimpleButtons at runtime:
// I have 4 MovieClips (button1, button2, etc) on the stage
for(var i:int = 1; i <= 4; i++)
{
var mc:MovieClip = getChildByName("button" + i) as MovieClip;
var button:SimpleButton = convertMovieClipToButton(mc);
button.tabIndex = i;
button.addEventListener(MouseEvent.CLICK, onClicked);
}
function convertMovieClipToButton(mc:MovieClip):SimpleButton
{
var className:Class = getDefinitionByName(getQualifiedClassName(mc)) as Class;
var button:SimpleButton = new SimpleButton(new className(), new className(), new className(), new className());
button.name = mc.name;
button.x = mc.x;
button.y = mc.y;
mc.parent.addChildAt(button, getChildIndex(mc));
mc.parent.removeChild(mc);
return button;
}
I have a movieClip and I want attach to this movieClip bitmaps. I want attach each bitmap to different frame of movieClip. I have tried something like this, but it is not working. It is for memory game I am creating.
for(var i : int = 0; i < cardList.length; i++){
var helpVar : int = cardList[i].pictureOfCard;
cardList[i].gotoAndStop(cardList[i].pictureOfCard+2);
var bitmap : Bitmap = new Bitmap(bitMapArray[helpVar].bitmapData.clone());
cardList[i].addChild(bitmap);
cardList[i].gotoAndStop(1);
}
You could simply load all Bitmaps and only show the one, that should be visible right now. e.g.
function ShowFrame(nr:int):void{
for(i:int = 0; i<bitMapArray.length; i++){
bitMapArray[i].visible = false;
}
bitMapArray[nr].visible = true
}
My AS3 skills are rusty, so this might need some syntax correction, but it works in theory.
var i :int = 0;
processNext();
function processNext():void
{
cardList[i].addEventListener(Event.FRAME_CONSTRUCTED, onFrameConstructed );
cardList[i].gotoAndStop(cardList[i].pictureOfCard+2);
}
function onFrameConstructed( e:Event ):void
{
cardList[i].removeEventListener(Event.FRAME_CONSTRUCTED, onFrameConstructed );
var helpVar : int = cardList[i].pictureOfCard;
var bitmap : Bitmap = new Bitmap(bitMapArray[helpVar].bitmapData.clone());
cardList[i].addChild(bitmap);
if( i < cardList.length - 1 )
{
i++;
processNext();
{
else
trace("All done");
}
After some time I discover that what I was trying to achieve is probably not possible in as3. The way I solved my problem is that everytime i gotoAndStop to frame I need have there bitmap I addChild(bitmap) and everytime I gotoAndStop other frame where there should not be bitmap I removeChild(bitmap) from movieClip. (So it is very similar aproach to makeing bitmap visible and invisible)
I'm gonna trying to explain as good as I can but, it's really hard to explain. I'm new to AS3 so if you are gonna help me, please help me til we solve it. Please paste code examples instead of just saying how I should do.
Ok.
On the main timeline I saying like this.
TIMER HERE THAT ADDS THE ENEMY EVERY SECOND!
var Enemy:MovieClip = new Enemy();
addChild(Enemy);
Enemy.x = 200;
Enemy.y = 200;
ANOTHER TIME THAT ADDS BULLETS EVERY .5 SECONDS!
var Bullet:MovieClip = new Bullet();
addChild(Enemy);
Bullet.x = 400;
Bullet.y = 400;
And then inside Enemy.as and Bullet.as I have code that says how it should travels, what speed etc. But how do I make a hitTest between these? I've tried to do it inside the enemy or bullet class like this.
So I basic asking for how I can hitTest two classes agianst each other? Or the object of a class?
You need to keep a reference on those enemies and bullets. Dont do var enemy:MovieClip = new Enemy(); instead do this.
var myEnemyList:Array = new Array();
var myBulletList:Array = new Array();
function Init():void{
addEventListener(Event.OnEnterFrame, Update);
}
function Update(){
//this will create a bullet and an enemy at every frame
//Create a new enemy
var enemy:Enemy = new Enemy();
myEnemyList.push(enemy); //add enemy to the array
//Create a new bullet
var enemy:Bullet = new Bullet();
myBulletlist.push(bullet);
//Update the bullets
for(var i:int=0; i < myBulletlist.length; i++){
myBulletlist[i].Update(); //you must implement this function inside your class bullet
}
//Update the enemies
for(var i:int=0; i < myEnemyList.length; i++){
myEnemyListt[i].Update(); //you must implement this function inside your class enemy
}
CheckForCollision();
}
function CheckForCollision(){
for(var i:int=0; i < myEnemyList.length; i++){
for(var j:int =0; j < myBulletList.length; j++){
if( myEnemyList[i].collidesWith(myBulletList[j]) ){
//Collision
}
}
}
}
Btw do not try to compile this its pretty much pseudo code. I'll answer questions you have. There's also a lot of tutorials everywhere on this, a little google search would help you get more specific code.