I have several movie clips into a frame the size of the stage and I have to switch through a button between those pages.
So if I press button, should all the other frames removeChild and the one where he is called to go addChild.
Edit: I have the actionscript placed in the timeline of the movieClip so the button is not on the stage but I put in the movie clip using action script.
So what DodgerThud showed here is not possible because the button has changed since that is in the movieClip('s).
I think I need to place the same code in every movieClip.
Put all of your MovieClips into a Vector or Array.
When you click the button, you should cycle through the Vector/Array and check if the MovieClip is currently on stage with contains(DisplayObject). If the Movieclip IS currently on the stage, remove it and add another one to the stage, for example, the next one in the Vector/Array.
var vec:Vector.<MovieClip> = new Vector.<MovieClip>
vec[0] = new MovieClip();
vec[1] = new MovieClip(); //example with MovieClips
vec[2] = new MovieClip();
addChild(vec[0]); //add one of the MovieClips to stage
button.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
for(var i:int = 0; i < vec.length; i++) //go through the Vector one by one
{
if(contains(vec[i]) //if the Object at position i in the Vector is on stage
{
removeChild(vec[i]); //remove the Object
var next:int = i; //create a temporary holder
if(next == vec.length) //check if the displayed Object was the last in the list
{
next = 0; //if so, set to 0
}else{
next++; //otherwise, only add 1
}
addChild(vec[next]); //add the next Object to the stage. If the removed Object was the last in the Vector, it'll add the first Object in the Vector to the list
break; //escape the for loop, otherwise it'll always only show the last Object
}
}
}
Something like ...
function tueHandler(e:MouseEvent):void
{
while(numChildren > 0)
removeChildAt(0);
addChild(whatever);
}
Related
Brand new to programming so I apologize in advance.
I'm trying to set up a game where after each time a button is pressed, a new instance of a MC is added to stage, then I need a collision detection for an existing object with any of the new instances of the MC. I need to add one instance to the stage at a time and once it reaches 8 instances, I need it to stop adding children but still be able to detect collisions with any of the instances. As it stands now it just replaces the MC or MCs into the new random position. Heres what my code looks like so far:
var pinkBox:pinkClass = new pinkClass();
var pinkArray:Array = new Array();
myButton.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownleftBtn);{
function mouseDownleftBtn(e: Event): void {
for(var i=0; i<8; i++)
{
addChild(pinkBox);
pinkArray.push(pinkBox);
pinkBox.x = Math.random()*stage.width;
}
myButton.startDrag();
}
}
addEventListener(Event.ENTER_FRAME, checkCollision);
function checkCollision(event:Event){
if (myButton.hitTestObject(pinkBox)) {
trace("hit")
}}
Lets say I have 30 objects created in for loop, added to a container.
Objects stop on frame 1. I have added event listeners to the objects as you can see below, and when I click any object inside container, it goes to frame 2 and play.
for (var i:int=0; i < 30; i++)
{
var object = new Object1();
object.gotoAndStop(1);
object.addEventListener(MouseEvent.CLICK, myFunction);
container.addChild(object);
}
private function myFunction(e:MouseEvent):void
{
e.currentTarget.gotoAndPlay(2);
}
So I have to click each object to send it on frame 2,
I also tried ROLL_OVER, everything is same but CLICK is changed to ROLL_OVER inside for loop.
What I want is to click, and then mouse over object so they go to frame 2 and play.
The problem is that I need to use MOUSE_DOWN event, I have tried to set MOUSE_DOWN instead of CLICK or ROLL_OVER, but it does not work. If I want to send objects to frame 2 (using MOUSE_DOWN), I need to click each of them, there is no difference between MOUSE_DOWN and CLICK in this case.
As someone who does not know much about mouse events, I'm wondering why roll over and click works, but mouse_down does not?
I think I see what you're trying to do... you want to press the mouse to start drawing over a bunch of sprites, each one goes to frame two when you mouse over it, but only if the mouse button is pressed, right?
try something like this
container.addEventListener(MouseEvent.MOUSE_DOWN, setMouseDown);
container.addEventListener(MouseEvent.MOUSE_UP, setMouseUp);
private var mouseIsDown:Boolean = false;
private var currentSprite:Sprite;
for (var i:int=0; i < 30; i++)
{
var object = new Object1();
object.gotoAndStop(1);
object.addEventListener(MouseEvent.MOUSE_OVER, myFunction);
object.mouseChildren = false;
container.addChild(object);
}
private function setMouseDown(e:MouseEvent){
mouseIsDown = true;
setActive(currentSprite);
}
private function setMouseUp(e:MouseEvent){
mouseIsDown = false;
}
private function myFunction(e:MouseEvent){
currentSprite = e.target;
if(mouseIsDown){
setActive(currentSprite);
}
}
private function setActive(target:Sprite){
target.gotoAndPlay(2);
}
I want to make a movieClip which has a reference of another movie clip move.
But when i do it it stays still.
Here is an example code i am trying to do
var movieClip1 = new movieClip();
var movieClip2 = new movieClip();
movieClip1.ref = movieClip2;
movieClip1.x = 0;
movieClip2.x = 0;
addChild(movieClip1);
addChild(movieClip2);
while(movieClip1.x !=300){
movieClip1.x +=1;
// i want to make the reference of the clip move as well
movieClip1.ref.x = movieClip1.x;
}
How are you wanting it to move? Progressively, 1 unit at a time? If so Don't use a while loop or it will simply jump straight to 300 (x position).
Instead use some sort of update loop like an enter frame listener:
addEventListener(Event.ENTER_FRAME, Update);
function Update(e:Event):void
{
mc1.x++;
mc1.ref.x = mc1.x;
}
I'm having a hard time with some code in actionscript 3.0. I don'y know how to randomize the movieclips stored on child and pick only 8 movieclips wherein there are 10 movieclips stored. I hope you'll be able to help me with this problem. thanks
Here is the code:
//start stage function
this.mainmc.addEventListener (Event.ENTER_FRAME, setupStage1);
this.waitingCounter=0;
//set up current stage
function setupStage1 (e:Event) {
//wait for timeline
if (this.waitingCounter<2) {
this.waitingCounter++;
//not ready yet, do nothing
return;
}
//Start the timer
timer.start();
//hide hint
this.mainmc.hintmc.visible=false;
//hide star animation
this.mainmc.starAnimation.visible=false;
//listener for hint button
this.mainmc.hintbut.addEventListener (MouseEvent.CLICK, showHint1);
//create objects array
this.obArr=[];
//count the objects on stage
for (var n=0; n<this.mainmc.numChildren; n++) {
//get the children
var ob=this.mainmc.getChildAt(n);
//only take movie clips
if (ob is MovieClip) {
//only count the movie clips that have name declared
if (ob.myname!=null) {
//push to array
this.obArr.push (MovieClip(ob));
}
}
}
on the code above, the code will store all the movieclips that are present in the stage. it stores them in a child. each 10 movieclips has a variable name "myname".
If you simply want to randomly sort items within an array, use the array.sort method and within your sort function, simply create a random number between 1 and 2. If it's 1, return true, if it's 2, return false. Here is an actionscript 2 snippet along with a link to a couple of tutorials:
var a:Array = new Array(“a”, “b”, “c”, “d”, “e”);
function shuffle(a,b):Number {
var num : Number = Math.round(Math.random()*2)-1;
return num;
}
var b:Array = a.sort(shuffle);
trace(b);
http://mrsteel.wordpress.com/2007/05/26/random-array-in-as2-as3-example-using-sort/
http://sierakowski.eu/list-of-tips/75-random-sort-function-comes-handy-when-building-applications-with-playlists.html
This is a much longer and more in-depth tutorial:
http://active.tutsplus.com/tutorials/actionscript/quick-tip-how-to-randomly-shuffle-an-array-in-as3/
I have a loop MC which will be duplicate to stage several times according to every click.
Now I want to detect if the MC being click belongs to which position in the array.
private function levelsBG():void {
LIST = new MovieClip();
d++;
for (var i:Number=0; i<myXML.children().length(); i++) {
listMC=new MovieClip();
LIST.addChild(listMC);
listMC.addEventListener(MouseEvent.CLICK,listClick);
}
listArray.push(LIST);
LISTmc.addChild(LIST);
addChild(LISTmc);
}
private function listClick(event:MouseEvent):void {
var currentListArray=listArray[listArray.length-1];
//trace from which position of Array
trace(listArray.length-event.target.parent)
}
why do you lookup the parent? maybe use mouseChildren = false, so event.target points to you clip ...
to find the index, use Array::indexOf, so should be something like trace(listArray.indexOf(event.target)) in the end ...