Copy link to clipboard
Copied
I have an assignment where I am to make a game similar to bloons where balls drop from the sky. However we've yet to be taught anything. I really need help with the array containing the images. I have a set of images;
blueBall
bombBall
greenBall
redBall
yellowBall
how can I add these into an array and addChild them in a random order and amount five in a row at a time?
Luke
1 Correct answer
change this:
for(var i:int=0;i<rowNum;i++){
shuffle(imageA);
for(var j:int=0;j<5;j++){
to:
for(var i:int=0;i<rowNum;i++){
shuffle(imageA);
for(var j:int=0;j<ballArray.length;j++){
Copy link to clipboard
Copied
are those library symbols or bitmaps (eg, png files) that need to be loaded or something else?
Copy link to clipboard
Copied
There images I've drawn myself on CS6 using the pen tool (rather than through code).
Thank you for replying
Copy link to clipboard
Copied
convert them to symbols (eg, movieclips) and assing each a linkage id (eg, BlueBall, BombBall, etc);
you can then use:
var imageA:Array=['BlueBall','BombBall',etc..];
var xgap:int = ?
var ygap:int = ?
var rowNum:int = ?
var C:Class;
var image:MovieClip;
for(var i:int=0;i<rowNum;i++){
shuffle(imageA);
for(var j:int=0;j<5;j++){
C=Class(getDefinitionByName(imageA
image=new C();
addChild(image);
image.y=i*ygap;
image.x=j*xgap;
}
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a
;
a
= t;
}
}
Copy link to clipboard
Copied
For now I've this; however I'm getting a non-null error, at first I thought that when i initiate my addChild the array was empty but it isnt;
for(var i:int = 1; i <= 25; i++)
{
if (randomBallGenerator == 0)
{
printBallArray.push(blueBall);
trace("Blue Ball Generated");
}
else if (randomBallGenerator == 1)
{
printBallArray.push(greenBall);
trace("Green Ball Generated");
}
else if (randomBallGenerator == 2)
{
printBallArray.push(redBall);
trace("Red Ball Generated");
}
else if (randomBallGenerator == 3)
{
printBallArray.push(yellowBall);
trace("Yellow Ball Generated");
}
else
{
trace("Number Not In Range")
}
//trace(printBallArray);
if (printBallArray.length == 25)
{
addChild(printBallArray);
}
}
I've got the decleration for some variables not shown here
Copy link to clipboard
Copied
use the suggestion in message 1.
Copy link to clipboard
Copied
Im using it but get the error : -
ReferenceError: Error #1065: Variable yellowBall is not defined.
at global/flash.utils::getDefinitionByName()
at Level1Class()
at GameManagerClass/GoToScene()
at HomeScreenClass/playButtonClicked()
Copy link to clipboard
Copied
you used yellowBall in the array and have no such linkage id.
Copy link to clipboard
Copied
Here's the full code;
package
{
//Import packages
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.SimpleButton;
import flash.utils.getDefinitionByName;
public class Level1Class extends SceneClass
{
//Background declaration.
var magicalDropIIBackground:MagicalDropIIBackground;
//Player declaration (Animated).
var character1IdleLeft:Character1IdleLeft;
var character1IdleRight:Character1IdleRight;
var character1WalkLeft:Character1WalkLeft;
var character1WalkRight:Character1WalkRight;
//Player speed.
var PlayerSpeed = 30;
//Ball declaration.
var blueBall:BlueBall;
var greenBall:GreenBall;
var redBall:RedBall;
var yellowBall:YellowBall;
var bombBall:BombBall;
//Ball Array.
//public var printBallArray:Array = new Array;
//Booleans for player face.
public function Level1Class()
{
// constructor code.
//Moveable Listeners.
GameManagerClass.getInstance().stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener)
GameManagerClass.getInstance().stage.addEventListener(KeyboardEvent.KEY_UP, keyUpListener)
GameManagerClass.getInstance().stage.addEventListener(Event.ENTER_FRAME, enterFrameListener);
//Background declaration.
magicalDropIIBackground = new MagicalDropIIBackground;
addChild(magicalDropIIBackground);
magicalDropIIBackground.x = 0;
magicalDropIIBackground.y = 0;
//Player declaration (Animated).
character1IdleLeft = new Character1IdleLeft;
character1IdleRight = new Character1IdleRight;
character1WalkLeft = new Character1WalkLeft;
character1WalkRight = new Character1WalkRight;
addChild(character1IdleRight);
character1IdleRight.scaleX = 0.5;
character1IdleRight.scaleY = 0.5;
character1IdleRight.x = 600;
character1IdleRight.y = 700;
//Ball declaration.
blueBall = new BlueBall;
greenBall = new GreenBall;
redBall = new RedBall;
yellowBall = new YellowBall;
bombBall = new BombBall;
//var ballArray:Array = new Array(blueBall, greenBall, redBall, yellowBall);
var ballArray:Array = ['blueBall','redBall','greenBall','yellowBall'];
var xgap:int = 50;
var ygap:int = 50;
var rowNum:int = 50;
var C:Class;
var image:MovieClip;
for(var i:int=0;i<rowNum;i++)
{
shuffle(ballArray);
for(var j:int=0;j<5;j++)
{
C=Class(getDefinitionByName(ballArray
image=new C();
addChild(image);
image.y=i*ygap;
image.x=j*xgap;
}
}
function shuffle(a:Array)
{
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--)
{
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a
;
a
= t;
}
}
}
override public function OnLeaveScene()
{
trace("leaving Game scene");
removeChildren();
this.removeEventListener(MouseEvent.CLICK, onClick);
//GameManagerClass.getInstance().stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown);
GameManagerClass.getInstance().stage.removeEventListener(Event.ENTER_FRAME, enterFrameListener);
}
public function onClick(e:MouseEvent)
{
trace("Game scene onClick");
GameManagerClass.getInstance().GoToScene("menu");
}
public function keyDownListener(e:KeyboardEvent)
{
trace ("X Location is " + this.x);
trace ("Y Location is " + this.y);
trace ("Key " + e.keyCode + " Pressed");
//Player Movement...
if (e.keyCode == 37)
{
character1IdleRight.x -= PlayerSpeed;
//this.gotoAndPlay()
}
if (e.keyCode == 39)
{
character1IdleRight.x += PlayerSpeed;
//this.gotoAndPlay()
}
}
public function keyUpListener(e:KeyboardEvent)
{
trace ("Key " + e.keyCode + " Released");
this.stop();
}
public function enterFrameListener(e:Event)
{
}
}
}
Im pretty certain its correct
Copy link to clipboard
Copied
it's not.
replace:
blueBall = new BlueBall;
greenBall = new GreenBall;
redBall = new RedBall;
yellowBall = new YellowBall;
bombBall = new BombBall;
//var ballArray:Array = new Array(blueBall, greenBall, redBall, yellowBall);
var ballArray:Array = ['blueBall','redBall','greenBall','yellowBall'];
with
var ballArray:Array = ['BlueBall','RedBall','GreenBall','YellowBall'];
p.s. your stage will need a height of, at least, 2500 px to fit 50 rows.
Copy link to clipboard
Copied
Sorry that I keep getting back to you with more problems, ArgumentError: Error #1507: Argument name cannot be null. This is the one now.
Copy link to clipboard
Copied
copy and paste the code that triggers that error. hint: use the code in message 3.
Copy link to clipboard
Copied
ArgumentError: Error #1507: Argument name cannot be null.
at global/flash.utils::getDefinitionByName()
at Level1Class()
at GameManagerClass/GoToScene()
at HomeScreenClass/playButtonClicked()
Its not saying what code is triggering it? But it could be from the C=Class(getDefinitionByName(ballArray
I used to work with flash but never touched arrays as I couldn't get along with them aha
Copy link to clipboard
Copied
change this:
for(var i:int=0;i<rowNum;i++){
shuffle(imageA);
for(var j:int=0;j<5;j++){
to:
for(var i:int=0;i<rowNum;i++){
shuffle(imageA);
for(var j:int=0;j<ballArray.length;j++){
Copy link to clipboard
Copied
My god you are a genius how can i go about adding more balls into the array? there are currently 2 positioned on top of each other
Copy link to clipboard
Copied
none should be on-stage to start. they should all be added via code.
if they're all added by code, none will be directly over another. they may overlap if your xgap and/or ygap aren't large enough.
you can add more objects to the array by creating more symbols and assigning a linkage id that you then add to the array.
(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)

