Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Making an array containing images and adding them to the stage HELP!!

New Here ,
Jan 09, 2017 Jan 09, 2017

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

TOPICS
ActionScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 10, 2017 Jan 10, 2017

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++){

Translate
Community Expert ,
Jan 09, 2017 Jan 09, 2017

are those library symbols or bitmaps (eg, png files) that need to be loaded or something else?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 09, 2017 Jan 09, 2017

There images I've drawn myself on CS6 using the pen tool (rather than through code).

Thank you for replying

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2017 Jan 09, 2017

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;

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 10, 2017 Jan 10, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2017 Jan 10, 2017

use the suggestion in message 1.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 10, 2017 Jan 10, 2017

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()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2017 Jan 10, 2017

you used yellowBall in the array and have no such linkage id.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 10, 2017 Jan 10, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2017 Jan 10, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 10, 2017 Jan 10, 2017

Sorry that I keep getting back to you with more problems, ArgumentError: Error #1507: Argument name cannot be null. This is the one now.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2017 Jan 10, 2017

copy and paste the code that triggers that error.  hint: use the code in message 3.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 10, 2017 Jan 10, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2017 Jan 10, 2017

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++){

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 10, 2017 Jan 10, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2017 Jan 10, 2017
LATEST

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.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines