ActionScript 3 after seven buttons clicked go to next scene - function

I have seven buttons (btn_one, btn_two, btn_three, ...) and when all of them were clicked (not in a row, just random) I want go to the first frame in the next scene. What does my code should look like?
Thank you!

const buttons:Array = [btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven];
// dictionary to keep clicks state
const clicked:Dictionary = new Dictionary();
function buttonClickHandler(event:Event):void {
// record specific button's click to a dictionary
clicked[event.currentTarget] = true;
// true, if all the dict keys have true values
const allButtonsClicked:Boolean = buttons.every(
function(button:Object, ...rest):Boolean {
return clicked[button];
});
if (allButtonsClicked) {
// remove all listeners
for each (var button:DisplayObject in buttons) {
button.removeEventListener(MouseEvent.CLICK, buttonClickHandler);
}
//do whatever action you need. For example:
gotoAndStop(/*neededFrame*/);
}
}
// initialize dictionary values, and add listeners
for each (var button:DisplayObject in buttons) {
clicked[button] = false;
button.addEventListener(MouseEvent.CLICK, buttonClickHandler);
}
haven't tested, it but should work. Anyway, the scheme should be valid.

stop();
const buttons:Array = [btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven];
// dictionary to keep clicks state
const clicked:Dictionary = new Dictionary();
function buttonClickHandler(event:Event):void {
// record specific button's click to a dictionary
clicked[event.currentTarget] = true;
// true, if all the dict keys have true values
const allButtonsClicked:Boolean = buttons.every(
function(button:Object, ...rest):Boolean {
return clicked[button];
});
if (allButtonsClicked) {
// remove all listeners
for each (var button:DisplayObject in buttons) {
button.removeEventListener(MouseEvent.CLICK, buttonClickHandler);
}
//do whatever action you need. For example:
gotoAndStop(2);
}
}
// initialize dictionary values, and add listeners
for (var button:DisplayObject in buttons) {
clicked[button] = false;
btn_one.addEventListener(MouseEvent.CLICK, buttonClickHandler);
btn_two.addEventListener(MouseEvent.CLICK, buttonClickHandler);
btn_three.addEventListener(MouseEvent.CLICK, buttonClickHandler);
btn_four.addEventListener(MouseEvent.CLICK, buttonClickHandler);
btn_five.addEventListener(MouseEvent.CLICK, buttonClickHandler);
btn_six.addEventListener(MouseEvent.CLICK, buttonClickHandler);
btn_seven.addEventListener(MouseEvent.CLICK, buttonClickHandler);
}
I tested the code of leetwinski and added the eventlisteners for my buttons, but it doesn't work. I get the error "1067: Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject" What did I do wrong?

Related

Flash CS4 How do you set variable to the Object Dragging Collison?

I am doing a game for my computer science class and I am working on inventory, dragging an item into it. I do not know how to set the item as whatever the user clicked to drag.
At the moment I hard coded it to the item they are dragging, but in the future I want more items, so a variable set to the item they are dragging would make it work perfectly, but I don't know what it's called to do that.
Here is my code for inventory item dragging
function dragItem (event:MouseEvent):void
{
knife_loot.startDrag();
}
function dropItem (event:MouseEvent):void
{
knife_loot.stopDrag();
if ((knife_loot.hitTestObject(inv_game)) && (inv_game.visible == true))
{
trace("Item dropped in inventory")
trace("")
knife_loot.x = 80
knife_loot.y = 120
}
}
// end of dragging and dropping items
You can start with:
// List the items you want to drag.
var aList:Array = [knife_loot, spoon_loot, fork_loot];
// InteractiveObject is superclass for SimpleButton, Sprite and MovieClip.
// If you're sure what they all are then just use their class instead.
for each (var anItem:InteractiveObject in aList)
{
// Subscribe them all for dragging.
anItem.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
}
public var draggedItem:InteractiveObject;
function onDrag(e:MouseEvent):void
{
// Use e.currentTarget because original MouseEvent e.target
// could be something from deep inside of top object e.currentTarget.
draggedItem = e.currentTarget as InteractiveObject;
draggedItem.startDrag();
// Let's hook drop events.
stage.addEventListener(Event.MOUSE_LEAVE, onDrop);
stage.addEventListener(MouseEvent.MOUSE_UP, onDrop);
}
function onDrop(e:Event):void
{
// Unhook drop events.
stage.removeEventListener(Event.MOUSE_LEAVE, onDrop);
stage.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
// Drop the item.
draggedItem.stopDrag();
if ((draggedItem.hitTestObject(inv_game)) && (inv_game.visible == true))
{
trace("Item", draggedItem.name, "was dropped to inventory.");
trace("");
draggedItem.x = 80;
draggedItem.y = 120;
}
// Forget the item.
draggedItem = null;
}

Action Script: Call corresponding functions on button click

I have been trying to add buttons dynamically and then assign event listeners to them. For which my code is,
var i=0;
var step=50;
for each (var child:XML in courseXML.footer.tray.elements())
{
var thumbClip:thumb = new thumb();
//set name
thumbClip.name = "mc_thumb" + (i + 1);
trace("thumbClipname:: "+thumbClip.name);
//set the x and y values
thumbClip.x = 620 + (i * step);
thumbClip.y = 560;
//attach the newly created instance to the container
addChild(thumbClip);
trace("thumbClip:: "+thumbClip);
//attach icon image from xml path
thumbClip.thumbLoader.source = child.icon;
trace("thumbClip.thumbLoader.source: "+thumbClip.thumbLoader.source);
//add listeners
trace("Node name "+ child.localName());
if(child.localName().toLowerCase() == "feedback");
{ trace("gotoFeedback..");
thumbClip.addEventListener(MouseEvent.CLICK, gotoFeedback);
}
if(child.localName().toLowerCase() == "resources");
{ trace("gotoResources..");
thumbClip.addEventListener(MouseEvent.CLICK, gotoResources);
}
if(child.localName().toLowerCase() == "glossary");
{ trace("gotoGlossary..");
thumbClip.addEventListener(MouseEvent.CLICK, gotoGlossary);
}
if(child.localName().toLowerCase() == "discussion");
{ trace("gotoDiscussion..");
thumbClip.addEventListener(MouseEvent.CLICK, gotoDiscussion);
}
thumbClip.buttonMode = true;
i++;
}
So, Finally 4 buttons are displayed on stage and when I click on any button all the four functions gotofeedback, gotoResources, gotoGlossary, gotoDiscussion are called. How do I call corresponding functions on button click? Thanks!
The four functions are:
function gotoFeedback(e: MouseEvent):void
{
trace("gotofeedback fn called..");
}
function gotoResources(e: MouseEvent):void
{
trace("gotoResources fn called..");
}
function gotoGlossary(e: MouseEvent):void
{
trace("gotoGlossary fn called..");
}
function gotoDiscussion(e: MouseEvent):void
{
trace("gotoDiscussion fn called..");
}
You need to add the event listeners to the child buttons, rather than to thumbClip. The way you have your code now, whenever the mouse is clicked anywhere inside thumbClip all the the listeners are called (which is what you are seeing).

Flash remove child from timer

I have 25 objects of movie clip class named drone, and when i click it, after 2 seconds I want the object to disappear. I also have 25 timers named countdown. Here is what i do:
function clickHandler (event:MouseEvent):void{
event.currentTarget.hp--;
if(event.currentTarget.hp <= 0)
{
for(var i:int = 0;i<25;i++)
{
if(event.currentTarget == _drone[i])
{
countdown[i].start(); //start timer
}
}
}
}
Here is my timer:
for(var i:int = 0;i<25;i++)
{
countdown[i] = new Timer(2000);
countdown[i].addEventListener(TimerEvent.TIMER,timerHandler);
}
function timerHandler(e:TimerEvent):void {
//remove the drone I clicked
//I also dont know which drone i'm clicking
}
What should I do in the timerHandler to remove the object I clicked?
You can use Dictionary. Use the timer as key and movielcip as value.
import flash.utils.Dictionary;
var dict:Dictionary = new Dictionary();
function clickHandler (event:MouseEvent):void{
event.currentTarget.hp--;
if(event.currentTarget.hp <= 0)
{
for(var i:int = 0;i<25;i++)
{
if(event.currentTarget == _drone[i])
{
dict[countdown[i]] = _drone[i];//set the target mc here
countdown[i].start(); //start timer
break;
}
}
}
}
function timerHandler(e:TimerEvent):void {
var mc:MovieClip = dict[e.target] as MovieClip;//get the object been clicked
if (mc && mc.parent) {
mc.parent.removeChild(mc);//remove it
}
}
With minimal changes, set up an array to track the drones:
var arrayToRemove:Array = new Array();
and then in the click handler store drones to be removed in there:
arrayToRemove.push(event.currentTarget);
and in the timerHandler just remove the first element of the array:
removeChild(arrayToRemove.shift());
Since every delay is the same the order of the events and removals will be preserved. Although, it would probably be better to generalize the code using the above example and store all drones and timers in an arrays, so you can have any number of them.

AS3 for transfering pauseposition of MC1 to MC2

Hi I'm quite a new on flash and need some code. I have 3 MCs say MC1, MC2 and MC3. I will have 3 dedicated buttons that will pause the music of one MC and transfer the frame position to another MC and start playing. so say for example if MC2 is playing and I press the MC3 button I would like it to take the pause position of MC2 (not MC1) and continue playing from that frame on MC3 as well as switching the visuals from MC2 to MC3. it's a Multilingual app and all 3 MC's have the same frame length. in other words I would like to switch between languages.
Thanks in advance, any help would be great.
EDIT: up till now I have
mtlyrvult.stop();
itlyrvult.stop();
engvult.addEventListener(MouseEvent.CLICK, playMC1);
function playMC1(e:MouseEvent):void {
itlyrvult.stop();
enlyrvult.gotoAndPlay(itlyrvult.currentFrame);
itlyrvult.gotoAndStop(1); //frame one is empty
engvult.mouseEnabled = false;
itvult.mouseEnabled = true;
}
itvult.addEventListener(MouseEvent.CLICK, playMC2);
function playMC2(e:MouseEvent):void {
enlyrvult.stop();
itlyrvult.gotoAndPlay(enlyrvult.currentFrame);
enlyrvult.gotoAndStop(1); //frame one is empty
itvult.mouseEnabled = false;
engvult.mouseEnabled = true;
}
This switches from one language to another. Now my client gave me another language. mtlyrvult. And I don't know how AS3 will recognise which mc is playing to take the the pauseposition/currentframe from it.
I stick to your code (no class, method, members, addChild, ...). I didn't try the final draft with Flash or SDK.
I assume that you have:
3 MovieClip objects : enlyrvult, itlyrvult, mtlyrvult;
3 InteractiveObject (SimpleButton, MovieClip, ...) objects : engvult, itvult, mtvult;
and that enlyrvult is playing when we begin here (if not: enlyrvult.play(); or enlyrvult.stop();).
itlyrvult.stop(); // or itlyrvult.gotoAndStop(1);
mtlyrvult.stop(); // or mtlyrvult.gotoAndStop(1);
engvult.addEventListener(MouseEvent.CLICK, playMC1);
itvult.addEventListener(MouseEvent.CLICK, playMC2);
mtvult.addEventListener(MouseEvent.CLICK, playMC3);
// play enlyrvult
function playMC1(e:MouseEvent):void {
// stop them
itlyrvult.stop();
mtlyrvult.stop();
// play me
enlyrvult.gotoAndPlay(mtlyrvult.currentFrame);
// hide them
itlyrvult.gotoAndStop(1); //frame one is empty
mtlyrvult.gotoAndStop(1); //frame one is empty
// My trigger is out, theirs are fine
engvult.mouseEnabled = false;
itvult.mouseEnabled = true;
mtvult.mouseEnabled = true;
}
// play itlyrvult
function playMC2(e:MouseEvent):void {
// stop them
enlyrvult.stop();
mtlyrvult.stop();
// play me
itlyrvult.gotoAndPlay(enlyrvult.currentFrame);
// hide them
enlyrvult.gotoAndStop(1); //frame one is empty
mtlyrvult.gotoAndStop(1); //frame one is empty
// My trigger is out, theirs are fine
itvult.mouseEnabled = false;
engvult.mouseEnabled = true;
mtvult.mouseEnabled = true;
}
// play mtlyrvult
function playMC3(e:MouseEvent):void {
// stop them
enlyrvult.stop();
itlyrvult.stop();
// play me
mtlyrvult.gotoAndPlay(itlyrvult.currentFrame);
// hide them
enlyrvult.gotoAndStop(1); //frame one is empty
itlyrvult.gotoAndStop(1); //frame one is empty
// My trigger is out, theirs are fine
mtvult.mouseEnabled = false;
engvult.mouseEnabled = true;
itvult.mouseEnabled = true;
}
OR, for as much (300?) as you want...
// add to your import:
import flash.utils.Dictionary;
// in your const/var section
const STARTING_FRAME:int = 1;
var dict = new Dictionary(); // mapping and memory
var currentTrack:MovieClip; // we will know who's last
initAll();
playTrack(enlyrvult, STARTING_FRAME, engvult);
function clickHandler(e:MouseEvent):void {
var playheadFrame:int = currentTrack.currentFrame; // we remember position
var trigger:InteractiveObject = (e.currentTarget as InteractiveObject); // who shot me ?
var nextTrack:MovieClip = (dict[trigger] as MovieClip); // who's next ?
resetAll(); // and again.. (http://en.wikipedia.org/wiki/Sisyphus)
playTrack(nextTrack, playheadFrame, trigger);
}
function playTrack(mc:MovieClip, fram:int, iObj:InteractiveObject):void {
currentTrack = mc;
currentTrack.gotoAndPlay(fram);
iObj.mouseEnabled = false;
}
function resetAll():void {
for (var key:InteractiveObject in dict) { key.mouseEnabled = true; }
for each (var value:MovieClip in dict) { value.gotoAndStop(1); } // diff-> each
}
function initAll():void {
dict[engvult] = enlyrvult;
dict[itvult] = itlyrvult;
dict[mtvult] = mtlyrvult;
//dict[avult] = alyrvult; //<- new one like this: dict[trigger]=lyrMC; add as much as you can!
for (var key:InteractiveObject in dict) {
key.addEventListener(MouseEvent.CLICK, clickHandler);
}
resetAll();
}

Accessing event.target or event.currentTarget beyond the listener Function

Originally I had two functions, the first function being dataLoaded which had an eventListener in it that spawned a new function called itemClicked. But if I wanted to add a delayedFunction and have itemClicked as an intermittent stage between dataLoaded and delayedFunction, how can I access the event.target or event.currentTarget of the original listener when I come to reference it in my delayedFunction?
private function dataLoaded(event:Event):void {
//items properties call - add other calls to master properties later on
items = data.item;
// parsing of each ingredient
for (var i = 0; i < items.length(); i++) {
// instantiation of mcItem (the stage for each item)
_item = new Item();
// sets //over// layer to invisible / transparent
_item.item_btn_over.alpha = 0;
// creates the var itemTextField
_itemTextField = new TextField();
_itemTextField.text = items[i].toString();
// adds textfield to displaylist
_item.addChild(_itemTextField);
//adds items to container displaylist
_container.addChild(_item);
}
_item.parent.addEventListener( MouseEvent.CLICK, itemClicked );
}
function itemClicked(event:MouseEvent):void {
if (event.target is Item) {
var item:Item = Item(event.target);
TweenLite.to(item.btn_delete, 1,{rotation:180});
setTimeout(delayedFunction,5000);
item.parent.removeChild(item);
parseXML(data);
}
}
function delayedFunction():void {
var item:Item = Item(event.target);
item.parent.removeChild(item);
}
(I've mocked up the functions above, so I may be missing some closing parenthesis, but this doesn't matter in relation to my question)
This question has been on my mind for days and I can't think of a way of making the event.target of the parent _item accessible to functions other than the listening function.
You can pass arguments to your setTimeout call like this:
setTimeout(delayedFunction, 5000, event.currentTarget);
Then get the argument from the arguments array from your delayedFunction method:
function delayedFunction():void {
var item:Item = arguments[0] as Item;
item.parent.removeChild(item);
}
Have a look at the setTimeout docs if you need information.