ActionScript 3.0 Array - actionscript-3

I have a working array and a loop with this array. I just have a trace action in my loop so I know that it is looping. My question is how would I be able to display the next image array when the right/wrong answer is selected. My code is below and any help would be appreciated :)
var characterArray:Array = new Array();
characterArray[0] = [pirateboy, pirategirl, pig]; //pig is the ans
characterArray[1] = [pirategirl, pirategirl ,pirateboy]; //pirateboy is the ans
characterArray[2] = [pirategirl, pirateboy, pirateboy]; //pirategirl is the ans
characterArray[3] = [parrot, snowman, parrot]; //snowman is the ans
pig.addEventListener(MouseEvent.CLICK, rightanswer);
function rightanswer (event:MouseEvent){
if (MovieClip(event.target) == characterArray[0][2])
{
gotoAndStop(3)
}
}
pirateboy.addEventListener(MouseEvent.CLICK, rightanswer1);
function rightanswer1 (event:MouseEvent){
if (MovieClip(event.target) == characterArray[1][2])
{
gotoAndStop(3)
}
}
for(var i:int = 0; i<3; i++) {
trace("game");
} //loops array

Your code needs some restructuring.
Store the index of the active characterArray subarray.
var characterArray:Array = new Array();
characterArray[0] = [pirateboy, pirategirl, pig]; //pig is the ans
characterArray[1] = [pirategirl, pirategirl ,pirateboy]; //pirateboy is the ans
characterArray[2] = [pirategirl, pirateboy, pirateboy]; //pirategirl is the ans
characterArray[3] = [parrot, snowman, parrot]; //snowman is the ans
pig.addEventListener(MouseEvent.CLICK, onCharacterClick);
pirateboy.addEventListener(MouseEvent.CLICK, onCharacterClick);
pirateGirl.addEventListener(MouseEvent.CLICK, onCharacterClick);
snowman.addEventListener(MouseEvent.CLICK, onCharacterClick);
var currentQuestion:int = 0;
function onCharacterClick(event:MouseEvent):void
{
switch(currentQuestion)
{
case 0:
if(event.target == pig)
{
currentQuestion++;
gotoAndStop(3);
}
break;
case 1:
if(event.target == pirateboy)
{
currentQuestion++;
gotoAndStop(3);
}
break;
case 2:
if(event.target == pirategirl)
{
currentQuestion++;
gotoAndStop(3);
}
break;
case 3:
if(event.target == snowman)
{
currentQuestion++;
gotoAndStop(3);
}
break;
default:
break;
}
}
This stores the current set of characters. You can access that specific array using characterArray[currentQuestion]. If you wanted to display each character choice, you can loop through the current characterArray like this:
for(var i:int = 0; i < characterArray[currentQuestion].length; i++)
{
//add characterArray[currentQuestion][i] to the displaylist
}

Related

Random movide clip without loop?

I created some MovieClips and put all of them on the stage. I'm trying to move a random MovieClip, using it only once, but at the same time I need to know which one it is because it will conflict with other tween effects. Is there an easy way to do that?
Here is what I've tried:
var biryukseklev1, ikiyukseklev1, ucyukseklev1, dortyukseklev1, besyukseklev1:int;
var assignavalue1, assignavalue2:int;
stage.addEventListener(Event.ENTER_FRAME,survivordondurlev6);
function survivordondurlev6(e:Event) {
if (biryerlessurlev1 == 0) {
biryukseklev1 = 764;//36
} else if (biryerlessurlev1 == 1) {
biryukseklev1 = 680;//120
} else if (biryerlessurlev1 == 2) {
biryukseklev1 = 596;//204
} else if (biryerlessurlev1 == 3) {
biryukseklev1 = 512;//288
} else if (biryerlessurlev1 == 4) {
biryukseklev1 = 428;//372
}
if (assignavalue1 == 0 && rabbitstatus.text.length < 2) { //Make sure to not used before
var my:Tween = new Tween(rabbit, "y", Back.easeInOut, rabbit.y, rabbit.y -biryukseklev1, 3, true);
rabbitstatus.text = "okfull";
} else if((assignavalue1 == 1 && birdstatus.text.length < 2){ //Make sure to not used before
var mys:Tween = new Tween(bird, "y", Back.easeInOut, bird.y, bird.y -biryukseklev1, 3, true);
birdstatus.text = "okfull";
}
}
If you want to keep track of which clips you've animated, then you'll have to have some way of iterating through them. The easiest list would be an Array, though how you mark them as "animated" is up to you. If you never plan on animating them again, then simply removing the ones that you've animated, and leaving the rest for later will work.
Below is example code, which you should be able to run in a new scene. You should be able to apply it to your needs with some small edits.
import fl.transitions.Tween;
import fl.transitions.easing.*;
// We'll keep track of which blocks still need to be animated by including them in this list
// Once we animate a block, we remove it from the list.
var pool:Array = [];
// Populate the stage with blocks.
for (var i:int = 0; i < 30; i++) {
var block:Sprite = createBlock();
addChild(block);
block.y = 50;
if (i > 0) { block.x = block.width * i; }
pool.push(block);
}
// Create an interactive button.
var txt:TextField = new TextField();
txt.text = "Do something";
addChild(txt);
txt.addEventListener("click", animateNext);
function createBlock():Sprite {
// Creates a single, randomly colored block.
var block:Sprite = new Sprite();
block.graphics.beginFill(random(0x000000, 0xFFFFFF));
block.graphics.drawRect(0,0,16,16);
block.graphics.endFill();
return block;
}
function random(low:Number, high:Number):Number {
// Returns a random number between the low and high numbers.
return Math.floor(Math.random() * (1+high-low)) + low;
}
function animateNext(e:Event):void {
// Do this 5 times.
for (var i:int = 0; i < 5; i++) {
// As long as we have blocks in the pool...
if (pool.length > 0) {
// Pick a random block from the pool
var index:int = random(0, pool.length-1);
// Animate it
new Tween(pool[index], "y", Back.easeInOut, pool[index].y, pool[index].y + 50, 3, true);
// Remove it from the pool so it isn't picked again
pool.splice(index, 1);
} else {
// Otherwise, break the loop.
break;
}
}
}

AS3 using arrow keys to highlight movieclips

I have an array with movieclips that I place on the stage. I want to use the keyboard arrow keys to change alpha of each movieclip separately, as if you are navigating trough them ( I hope this makes sence).
So far I can only highlight them all at once using the UP/DOWN arrow.My goal is to loop trough them with highlight and downlight using alpha property.
This is my code:
import flash.events.KeyboardEvent;
import flash.events.Event;
var num1: Number = 262;
var aantal: Number = 8;
function Main() {
var BTN_arr: Array = new Array();
var houder: Number = 1;
var aantal2: uint = BTN_arr.length;
var nextBTN: uint;
var currentBTN: uint;
for (var i = 0; i < aantal; i++) {
var myBTN: BTNBg = new BTNBg();
myBTN.name = "btn" + i;
BTN_arr.push(myBTN);
addChild(myBTN);
myBTN.alpha = .45;
myBTN.x = 40;
myBTN.y = num1;
num1 += 90;
}
BTN_arr[0].alpha = 1;
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.DOWN) {
for (var i = 0; i < BTN_arr.length; i++) {
BTN_arr[i].alpha = 1;
}
}
trace("down");
if (e.keyCode == Keyboard.UP) {
for (var j = 0; j < BTN_arr.length; j++) {
BTN_arr[j].alpha = .45;
}
trace("up");
//MyBTN.alpha = 1;
}
}
}
Main();
var position:int = 0;
function updateHighlight() : void{
BTN_arr[position].alpha = 1; //highlight new one
}
function myKeyDown(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.DOWN) {
trace("down");
if(position > 0){
BTN_arr[position].alpha = .45; //unhighlight current
position--;
updateHighlight();
}
}
if (e.keyCode == Keyboard.UP) {
trace("up");
if(position < BTN_arr.length - 1){ //is not the last one
BTN_arr[position].alpha = .45; //unhighlight current
position++;
updateHighlight();
}
}
}
To do what you want, you don't need to use a for loop every time to set buttons alphas, so you can use a var to set the current button and you set alpha just to it, like this :
var current_button:int = 0;
var sens:int = 0; // -1 : up, 1 : down
stage.addEventListener(Event.ENTER_FRAME, _onEnterFrame)
function _onEnterFrame(e:Event):void {
if(sens != 0){
BTN_arr[current_button].alpha = .45;
if(0 <= current_button + sens && current_button + sens < BTN_arr.length) current_button += sens;
// if you want to pass from the last button to the first one and vice versa, you can enable these 3 lines and disable the 1st if
//current_button += sens;
//if(current_button < 0) current_button = BTN_arr.length - 1;
//else if(current_button >= BTN_arr.length) current_button = 0;
BTN_arr[current_button].alpha = 1;
sens = 0;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
function _onKeyDown(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.DOWN) {
sens = 1;
} else if (e.keyCode == Keyboard.UP) {
sens = -1;
}
}
Which will give you something like this, and like this for the 2nd case (with 3 commented lines enabled).
Hope that can help.

Objects to bounce around the screen?

I would like to make the 5 'burger' objects bounce around the screen so they are harder to shoot as is the aim of my game. But, so far they are only lining up at the top of the stage so it's way too easy to play. Would I need to create 5 separate objects with 5 separate instance names etc.
This is what I have so far:
var firing:Boolean = false;
var bullet:Bullet1 = new Bullet1();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
function keydown(event:KeyboardEvent):void {
switch(event.keyCode) {
case Keyboard.LEFT :
ball.x -= 10;
break;
case Keyboard.SPACE :
if (!firing) {
fire();
}
break;
case Keyboard.RIGHT :
ball.x += 10;
break;
case Keyboard.UP :
ball.y -= 10;
break;
case Keyboard.DOWN :
ball.y += 10;
break;
default :
break;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
function fire() {
addChild(bullet);
firing = true;
bullet.x = ball.x;
bullet.y = ball.y - 60
;
}
addEventListener(Event.ENTER_FRAME, movestuff);
function movestuff(event:Event):void {
if (firing) {
bullet.y -= 20;
if (bullet.y < 0) {
firing = false;
removeChild(bullet);
}
}
}
var numBurger:Number = 5;
var array:Array = new Array();
for (var i:uint = 0; i<numBurger; i++) {
var burger:Burger = new Burger();
array.push(burger);
addChild(burger);
burger.x = 100 + 100*i;
burger.y = 50;
}
addEventListener(Event.ENTER_FRAME, checkCollision);
function checkCollision(event:Event)
{
for (var i:uint=0; i<array.length; i++)
{
if (array[i].hitTestObject(bullet))
{
removeChild(array[i]);
array.splice(i,1);
return;
}
}
}
Thanks for any help.
No, you would not have to create each movie clip separately if you use a loop to create randomized x and y locations for each burger. You can also use Math.random() to give a random speed and direction to each burger. In the code below these values are held in "direction_ary" array. This code creates five MovieClips of the "Burger"class, and places them at random points on the screen. The code also creates random speeds and directions for each MovieClip:
import flash.events.Event;
function find_random(max,min){
return Math.round(min+(max-min)*Math.random());
}
var ary:Array = [];
var direction_ary:Array = [];
for(var i:uint=0;i<5;i++){
ary[i]=new Burger();
ary[i].name="burger"+(i);
ary[i].x=find_random(stage.stageWidth-ary[i].width,ary[i].width);
ary[i].y=find_random(stage.stageHeight-ary[i].height,ary[i].height);
addChild(ary[i]);
direction_ary[i]=[find_random(5,-5),find_random(5,-5)];
for(var e:uint=0;e<100;e++){
if(direction_ary[i][0]==0||direction_ary[i][1]==0){
direction_ary[i]=[find_random(5,-5),find_random(5,-5)];
}else{
break;
}
}
}
stage.addEventListener(Event.ENTER_FRAME,update_burgers);
function update_burgers(e:Event){
for(var i:uint=0;i<5;i++){
if (ary[i].x>stage.stageWidth||ary[i].x<0){
direction_ary[i][0]*=-1;
}
if (ary[i].y>stage.stageHeight||ary[i].y<0){
direction_ary[i][1]*=-1;
}
ary[i].x+=direction_ary[i][0];
ary[i].y+=direction_ary[i][1];
}
}
The code is fairly self explanatory. Good luck with your project.
Cheers,
Drake Swartzy

Issue with for loop in an Array

Initially the array holds the default values as 100. Once if the enemy looses its health then i want to set the default values from 100 to 0. When all the array elements gets a value of 0 then the message will trace as game win.
var enemyHealth:Array = new Array(100,100,100,100,0);
for (var i = 0; i< enemyHealth.length; i++)
{
if (enemyHealth[i] == 0)
{
trace ("game win");
}
}
Actually the issue is if any one of the array element holds a value of 0 then it trace a massage as game win.
So can any one help me in this regard.
You need to check all elements, not just one:
var allZero:Boolean = true;
var enemyHealth:Array = new Array(100,100,100,100,0);
for (var i = 0; i< enemyHealth.length; i++)
{
if (enemyHealth[i] != 0)
{
allZero = false;
break; // We know it's not a win, so we can stop the loop early
}
}
if (allZero) {
trace ("game win");
}
I would do something like this
var enemyHealth:Array = new Array(100,100,100,100,0);
var isDead:Boolean = true;
for (var i = 0; i< enemyHealth.length; i++)
{
if (enemyHealth[i] != 0)
{
isDead = false;
break;
}
}
if(isDead)
{
trace ("Game Win");
}
You can do what the other answerers have said or something like this which might help you more to get the exact amount of enemies dead.
var enemyHealth:Array = new Array(100,100,100,100,0);
var enemiesDead:int=0;
for (var i = 0; i< enemyHealth.length; i++)
{
if (enemyHealth[i] == 0)
{
enemiesDead++;
}
}
if(enemiesDead==enemyHealth.length)
{
trace("Game Over");
}
You can use the every method to check that every element of your array (or vector) meet a criterion.
const enemyHealth:Vector.<uint> = new <uint>[100,100,100,100,0];
const zeroHealth:Function = function(item:uint, index:int, vector:Vector.<uint>):Boolean {
return item === 0;
}
if (enemyHealth.every(zeroHealth)) {
trace("Game win")
}
I have changed the array to a Vector because they are more efficient, and you can specify the type of the elements, but it also works fine with array.

AS3: Large number of timers lagging flash

I have a large number of objects (472) that require a timer used to check how long it takes between state changes of a variable. The code below is what I have so far but that many timers running definitely impacts the performance of the application, is there a better optimised way of measuring this?
import flash.utils.Timer;
var active_:Boolean;
var matched:Boolean;
var vacated:Boolean;
var circ:Shape=new Shape();
this.addChild(circ);
circ.x = 0;
circ.y = 0;
var circRad:Number = 5;
var mat= new Matrix();
var busyColors = [0xFFFF00,0xFFCC00];
var idleColors = [0xCCCCCC,0x000000];
var matchedColors = [0x0099FF,0x0066FF];
var vacatedColors = [0xFF0000,0x990000];
var busyAlphas = [1,1];
var idleAlphas = [0.5,0.5];
var ratios = [0,255];
var prev:int = 0;
var time:Timer = new Timer(1000,0);
//time.start();
mat.createGradientBox(2*circRad,2*circRad,0,-circRad,-circRad);
circ.graphics.lineStyle();
if (active_ == false)
{
if (prev != 0)
{
setAverage(prev,false);
prev = 0;
}
circ.graphics.clear();
circ.graphics.beginGradientFill(GradientType.RADIAL,idleColors,idleAlphas,ratios,mat);
circ.graphics.drawCircle(0,0,circRad);
circ.graphics.endFill();
}
else if (active_ == true && matched == true)
{
if (prev != 1)
{
setAverage(prev,true);
prev = 1;
}
circ.graphics.clear();
circ.graphics.beginGradientFill(GradientType.RADIAL,matchedColors,busyAlphas,ratios,mat);
circ.graphics.drawCircle(0,0,circRad);
circ.graphics.endFill();
}
else if (active_ == true && vacated == false && matched == false)
{
if (prev != 2)
{
setAverage(prev,true);
prev = 2;
}
circ.graphics.clear();
circ.graphics.beginGradientFill(GradientType.RADIAL,busyColors,busyAlphas,ratios,mat);
circ.graphics.drawCircle(0,0,circRad);
circ.graphics.endFill();
}
else if (active_ == true && vacated == true)
{
if (prev != 3)
{
setAverage(prev,true);
prev = 3;
}
circ.graphics.clear();
circ.graphics.beginGradientFill(GradientType.RADIAL,vacatedColors,busyAlphas,ratios,mat);
circ.graphics.drawCircle(0,0,circRad);
circ.graphics.endFill();
}
function setAverage(i:int, a:Boolean)
{
time.stop();
switch (i)
{
case 0 :
break;
case 1 :
MovieClip(root).avgMat.push(uint(time.currentCount));
break;
case 2 :
MovieClip(root).avgBusy.push(uint(time.currentCount));
break;
case 3 :
MovieClip(root).avgVac.push(uint(time.currentCount));
break;
}
if(a == true){
//time.reset();
//time.start();
}
}
Commented out the timer starts because of the issue.
You can make public function like "tick()" at yours objects and call it in loop from 1 general timer outside.
Consider doing the following:
in the top, import getTimer
import flash.utils.Timer;
Create a variable containing the starting time (instead of doing timer.start) do this:
var timerStart : int = getTimer();
and instead of doing this: time.currentCount
Simply do this:
(getTimer() - timerStart) / 1000
Good luck.
you could change all the timers with addEventListener of type EVENT FRAME
var counter:Number = 0;
addEventListener(Event.ENTER_FRAME, counting);
function counting (e:Event):void
{
counter++
}
So counter will increment on every frame. Now you have to find fps. Now multiply fps by time that you want timer to run after. So it's should look like this:
var counter:Number = 0;
addEventListener(Event.ENTER_FRAME, counting);
function counting (e:Event):void
{
counter++
if(counter == 60) //60 fps means this line will run after 1 sec.
{
//do something
}
If(counter == 120) //60 fps; 2 sec
{
//do something
}
}
This method is much more accurate and the system will not be so overloaded. This can especcially help you for setting up the stage using addChild, when you need a delay after the animation is started.
You can make more of this 'timers', but that depends on the demands of the game, but i'm sure that it will work faster than hundreths of timers.