Turn this AS3 into an array and work with it - actionscript-3

How can I turn this basic function method into an Array and call to easily. Pretty much I am just comparing if objects have become false and then do something...but this just seems like a lot of code for something so easy. any ideas?
var b:Boolean = true;
var i:Boolean = true;
var t:Boolean = true;
var a:Boolean = true;
var m:Boolean = true;
var ii:Boolean = true;
var n:Boolean = true;
var e:Boolean = true;
var s:Boolean = true;
level5_mc.let_b_mc.addEventListener(MouseEvent.CLICK, hitB);
level5_mc.let_i_mc.addEventListener(MouseEvent.CLICK, hitI);
level5_mc.let_t_mc.addEventListener(MouseEvent.CLICK, hitT);
level5_mc.let_a_mc.addEventListener(MouseEvent.CLICK, hitA);
level5_mc.let_m_mc.addEventListener(MouseEvent.CLICK, hitM);
level5_mc.let_ii_mc.addEventListener(MouseEvent.CLICK, hitII);
level5_mc.let_n_mc.addEventListener(MouseEvent.CLICK, hitN);
level5_mc.let_e_mc.addEventListener(MouseEvent.CLICK, hitE);
level5_mc.let_s_mc.addEventListener(MouseEvent.CLICK, hitS);
function hitB(event:MouseEvent){
b=false;
trace("good");
level5_mc.removeChild(level5_mc.let_b_mc);
}
function hitI(event:MouseEvent){
if (b==false){
i=false;
level5_mc.removeChild(level5_mc.let_i_mc);
}
else {
//decrease timer
i=true;
}
}
function hitT(event:MouseEvent){
if (b==false && i==false){
t=false;
level5_mc.removeChild(level5_mc.let_t_mc);
}
else {
//decrease timer
i=true;
}
}
and so forth... edited below
var b=level5_mc.let_b_mc;
var i=level5_mc.let_i_mc;
var t=level5_mc.let_b_mc;
var movieClips:Array = [b,i,t];
var movieClipFlags:Object = {
b:[],
i:[b],
t:[b,i]
};
for each(var mc:MovieClip in movieClips) {
mc.addEventListener(MouseEvent.CLICK,movieClipHit);
}
function movieClipHit(e:MouseEvent) {
var mc:MovieClip = e.target as MovieClip;
if(readyToRemove(mc))
level5_mc.removeChild(mc);
else
trace("Can't remove yet.");
}
function readyToRemove(mc:MovieClip):Boolean {
for each(var mc:MovieClip in movieClipFlags[mc]) {
//if it has parent, it isn't removed yet.
if(mc.parent)
return false;
}
return true;
}

Something like the following should give you some ideas. I haven't tested the code you migt need to make some adjustments depending on your needs.
//put other movies clips in this array too.
var movieClips:Array = [level5_mc.let_b_mc,level5_mc.let_i_mc];
var movieClipFlags:Object = {
level5_mc.let_b_mc:[],
level5_mc.let_i_mc:[level5_mc.let_b_mc],
level5_mc.let_t_mc:[level5_mc.let_b_mc,level5_mc.let_i_mc]
//put other movie clips here too with the movie clips that need to be removed in the array
};
foreach(var mc:MovieClip in movieClips) {
mc.addEventListener(MouseEvent.CLICK,movieClipHit);
}
private function movieClipHit(e:MouseEvent) {
var mc:MovieClip = e.target as MovieClip;
if(readyToRemove(mc))
level5_mc.removeChild(mc);
else
trace("Can't remove yet.");
}
private function readyToRemove(mc:MovieClip):Boolean {
foreach(var mc:MovieClip in movieClipFlags[mc]) {
//if it has parent, it isn't removed yet.
if(mc.parent)
return false;
}
return true;
}
Hope it helps.

Related

Set Value and Position of Range Slider with Ionic

Hy,
I am using the Ionic Framework for App Development and i would like to track the time of a song with a Range Slider. I can see the value changes correctly int the developer tools, but the value gets not applied to the visual representation of the slider. Is it even possible to do that?
part of my js code so far:
var duration, playerWasPaused;
var blockSeek = false;
var player;
var seekBar;
var url = 'http://xx.mp3';
function init(){
console.log('Init');
player = new Audio();
player.src = url3;
player.oncanplay = PreparePlay;
player.volume = 0.2;
player.currentTime = 0;
seekBar = $('#playerRange');
player.play();
}
function PreparePlay(){
$("#playerRange").attr("min", 0);
$("#playerRange").attr("max",player.duration);
if(seekBar){
createSeekBar(player, seekBar);
}
}
function createSeekBar(seekBar){
player.addEventListener('loadedmetadata', enableDisableSeekBar, false);
player.addEventListener('emptied', enableDisableSeekBar, false);
player.addEventListener('timeupdate', onTimeupdate, false);
player.addEventListener('input', onSeek, false);
player.addEventListener('change', onSeekRelease, false);
enableDisableSeekBar();
onTimeupdate();
}
function enableDisableSeekBar(){
duration = player.duration;
if(duration && !isNaN(duration)){
seekBar.max = duration;
seekBar.disabled = false;
} else {
seekBar.disabled = true;
}
}
function onSeek(){
if(!blockSeek){
blockSeek = true;
playerWasPaused = player.paused;
player.pause();
}
player.currentTime = seekBar.value;
}
function onSeekRelease(){
if(!playerWasPaused){
player.play();
}
blockSeek = false;
}
function onTimeupdate(){
if(!blockSeek){
seekBar.attr("value", player.currentTime);
}
}
Source: http://jsfiddle.net/trixta/MfLrW/
I solved my problem with this call
seekBar.val(player.currentTime);
in the onTimeupdate.

Disable stop the object when it is hit on the right position

I want to be a drag and drop game.When I will to start drag my object be a small size to a drop position.It is performed correctly.But when drag object is touched hit point, it could not stop drag. Can I stop drag?Here is my code.....
drag_6.buttonMode = true;
drag_6.addEventListener(MouseEvent.MOUSE_UP, dropMe6);
drag_6.addEventListener(MouseEvent.MOUSE_DOWN, dragMe6);
var back_6X:Number = back_6.x;
var back_6Y:Number = back_6.y;
var hit_6X:Number = hit_6.x;
var hit_6Y:Number = hit_6.y;
var drop_6H:Number = drop_6.height;
var drop_6W:Number = drop_6.width
function dragMe6(event:MouseEvent)
{
drag_6.startDrag();
drag_6.height=drop_6H;
drag_6.width=drop_6W;
setChildIndex(drag_6, this.numChildren-1);
}
function dropMe6(event:MouseEvent)
{
drag_6.stopDrag();
if (drag_6.hitTestObject(drop_6))
{
TweenMax.to(drag_6, 0.5, {x:hit_6X, y:hit_6Y, ease:Cubic.easeOut});
drag_6.mouseEnabled = false;
SoundMixer.stopAll();
drag_6.alpha = 0 ;
hit_6.alpha = 1;
drag_6.buttonMode = false;
}
else
{
TweenMax.to(drag_6, 0.5, {x:back_6X, y:back_6Y, ease:Bounce.easeOut});
SoundMixer.stopAll();
}
}
The reason is the mouseup event is not firing at all. The best a simple way to add event listeners to stage. `
drag_6.buttonMode = true;
drag_6.addEventListener(MouseEvent.MOUSE_DOWN, dragMe6);
var back_6X:Number = back_6.x;
var back_6Y:Number = back_6.y;
var hit_6X:Number = hit_6.x;
var hit_6Y:Number = hit_6.y;
var drop_6H:Number = drop_6.height;
var drop_6W:Number = drop_6.width
function dragMe6(event:MouseEvent)
{
stage.addEventListener(MouseEvent.MOUSE_UP, dropMe6);
drag_6.startDrag();
drag_6.height=drop_6H;
drag_6.width=drop_6W;
setChildIndex(drag_6, this.numChildren-1);
}
function dropMe6(event:MouseEvent)
{
stage.removeEventListener(MouseEvent.MOUSE_UP, dropMe6);
drag_6.stopDrag();
if (drag_6.hitTestObject(drop_6))
{
TweenMax.to(drag_6, 0.5, {x:hit_6X, y:hit_6Y, ease:Cubic.easeOut});
drag_6.mouseEnabled = false;
SoundMixer.stopAll();
drag_6.alpha = 0 ;
hit_6.alpha = 1;
drag_6.buttonMode = false;
}
else
{
TweenMax.to(drag_6, 0.5, {x:back_6X, y:back_6Y, ease:Bounce.easeOut});
SoundMixer.stopAll();
}
}
Make sure the global stage object is accessible.

How can i check by actionscript 3 if every items of an array had hitTestObject the items of the second array

I have this code for a drag and drop game.
How can i check by actionscript 3 if every item of an array had hitTestObject the items of the second array so something else can happens for example show a well done message.
var hitArray:Array = new Array(hitTarget1,hitTarget2,hitTarget3,hitTarget4,hitTarget5);
var dropArray:Array = new Array(drop1,drop2,drop3,drop4,drop5);
var positionsArray:Array = new Array();
for (var i:int = 0; i < dropArray.length; i++) {
dropArray[i].buttonMode = true;
dropArray[i].addEventListener(MouseEvent.MOUSE_DOWN, mdown);
dropArray[i].addEventListener(MouseEvent.MOUSE_UP, mUp);
positionsArray.push({xPos:dropArray[i].x, yPos:dropArray[i].y});
}
function mdown(e:MouseEvent):void {
e.currentTarget.startDrag();
setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
}
function mUp(e:MouseEvent):void {
var dropIndex:int = dropArray.indexOf(e.currentTarget);
var target:MovieClip = e.currentTarget as MovieClip;
target.stopDrag();
if (target.hitTestObject(hitArray[dropIndex])) {
target.x = hitArray[dropIndex].x;
target.y = hitArray[dropIndex].y;
playSound(sosto);
}else{
target.x = positionsArray[dropIndex].xPos;
target.y = positionsArray[dropIndex].yPos;
}
}
reset.addEventListener(MouseEvent.CLICK, backObjects);
function backObjects(e:MouseEvent):void{
for(var i:int = 0; i < dropArray.length; i++){
if(dropArray[i].x == hitArray[i].x && dropArray[i].y == hitArray[i].y){
dropArray[i].x = positionsArray[i].xPos;
dropArray[i].y = positionsArray[i].yPos;
}
}
}
function playSound(SoundName:Class):void{
var sound = new SoundName();
var channel:SoundChannel = sound.play();
}
playSound(sosto);
Let's create a function to tell us if every "hitTarget" and its corresponding "drop" are touching each other.
function allCorrect():Boolean {
//for each item in the hitArray, we repeat the action
//we assume that length of hitArray and dropArray are the same
for (var i=0;i<hitArray.length;i++) {
//if the corresponding target and drop do not hit each other, return false
if (!(hitArray[i].hitTestObject(dropArray[i])) {
return false
//if we return false, this function will continue no further
}
}
//We have cycled through all of the items in the arrays, and none of the
//hitTestObjects have been false, so we return true
return true
}
We want to test if everything is correct when we update the positions of our "drops". So, in your mUp function, add this line to the bottom
if (allCorrect()) {
//display "youz a cool cat, bro"
//remove all of your event listeners to improve performance
}
hope this answers your question!
Try adding this line:
stage.addEventListener(Event.ENTER_FRAME, loop);
Where "loop" is a function that contains the rest of your code, which looks something like...
function loop (e:Event):void
{
//Your Code
}
Good luck!

swap depthes when movie clips loaded from library

I have two movie clips in the library with linkage.
on the stage I have two buttons - each load a movie clip to a specific mc target on the stage.
I also have a third button that removes the mc target, to clear the stage.
I want to know how can I change the code in AS3 so the loaded movie clips will not show at the same time, but swap each other, like I used to use depth in AS2.
This is the code:
var myIgool = new igool ();
var myRibooa = new ribooa ();
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
mc_all.addChild (myIgool);
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
mc_all.addChild (myRibooa);
}
unloadall.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_6);
function fl_MouseClickHandler_6(event:MouseEvent):void
{
removeChild(mc_all);
;
}
I would recommend something like this:
var myIgool = new igool ();
var myRibooa = new ribooa ();
mc_all.addChild(myIgool);
mc_all.addChild(myRibooa);
myIgool.visible = false;
myRibooa.visible = false;
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
myIgool.visible = true;
myRibooa.visible = false;
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
myIgool.visible = false;
myRibooa.visible = true;
}
unloadall.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_6);
function fl_MouseClickHandler_6(event:MouseEvent):void
{
myIgool.visible = false;
myRibooa.visible = false;
}
But if you really want to swap, you could also do the following, however I recommend setting the visible flag as it's more efficient rather than covering something up that wouldn't need to draw.
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
if (myIgool.parent != mc_all)
{
mc_all.addChild(myIgool);
}
else
{
mc_all.setChildIndex(myIgool, mc_all.numChildren - 1);
}
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
if (myRibooa.parent != mc_all)
{
mc_all.addChild(myRibooa);
}
else
{
mc_all.setChildIndex(myRibooa, mc_all.numChildren - 1);
}
}

Actionscript 3.0 Keyboard event listener error

I have two functions, rotate and unrotate, and I used a keyboard event listener to listen for key_Down A and B but i get and error:
1119: Access of possibly undefined property A through a reference with static type Class.
ti.border = true
ti.addEventListener(TextEvent.TEXT_INPUT, onInput);
function onInput(event:TextEvent):void {
if(ti.text.search('a')!=-1) load_image("http://i54.tinypic.com/anom5d.png", "ottefct");
else if(ti.text.search('b')!=-1) load_image("http://i53.tinypic.com/2dv7dao.png", "rnd");
else if(ti.text.search('c')!=-1) load_image("http://i51.tinypic.com/m8jp7m.png", "ssd");
}
var loaded_images:Dictionary = new Dictionary();
function load_image(url:String, id_name:String)
{
var loader:Loader = new Loader();
loader.name = id_name;
var url_req:URLRequest = new URLRequest(url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
loader.load(url_req);
}
function onLoadingComplete(evt:Event):void
{
var img_name:String = evt.currentTarget.loader.name
var spr_box:Sprite = new Sprite();
spr_box.addChild(evt.currentTarget.loader);
spr_box.mouseChildren = false;
spr_box.doubleClickEnabled = true;
spr_box.addEventListener(MouseEvent.MOUSE_DOWN, drag);
spr_box.addEventListener(MouseEvent.MOUSE_UP, drop);
spr_box.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
spr_box.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);
spr_box.width = 124;
spr_box.height = 180;
spr_box.x = 430;
spr_box.y = 425;
this.addChild(spr_box);
loaded_images[img_name] = spr_box;
}
function drag(evt:MouseEvent):void
{
evt.currentTarget.startDrag()
}
function drop(evt:MouseEvent):void
{
evt.currentTarget.stopDrag()
}
function rotate(evt:KeyboardEvent):void
{
if (evt.keyCode==Keyboard.D) {
evt.currentTarget.rotation = 90
}
}
function unrotate(evt:KeyboardEvent):void
{
if (evt.keyCode==Keyboard.A) {
evt.currentTarget.rotation = 0
}
}
Well - you are only referencing prop A in one spot and its throwing an undefined error. So, either your not linking the Keyboard class properly... or...?
At any rate - you can also use numeric assignment to capture keystrokes. In this case:
function rotate(evt:KeyboardEvent):void
{
if (evt.keyCode == 68) { //"D"
evt.currentTarget.rotation = 90
}
}
function unrotate(evt:KeyboardEvent):void
{
if (evt.keyCode == 65) { //"A"
evt.currentTarget.rotation = 0
}
}
They are all laid out pretty clearly right here.