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

I need help trying to write my code to find object inside another object.

Community Beginner ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

I am using AS3 in Flash Professional CS5.5

i get these errors when I put my "boxMC" into the background of "WorldMC". it works not how i want it when i put put boxMC on my main stage. When i do so it only traces for that one boxMC not the ones that are spawning inside of WorldMC.

Here are my errors:

1067: Implicit coercion of a value of type Array to an unrelated type tankscript.turrent:Box.

1180: Call to a possibly undefined method Box.

1061: Call to a possibly undefined method push through a reference with static type tankscript.turrent:Box

1119: Access of possibly undefined property length through a reference with static type tankscript.turrent:Box.

Here is my code that is having the issues.

  var boxMC:Array;

  WorldMC.boxMC = new Array();

  //------------------------------

  // -----Box Spawning------

  //------------------------------

  //only spawn a box if there are less tan 100 already on screen

  if(WorldMC.numChildren<10)

  {

  // Make a new instance of the box class

  var bx = new Box();

  // add the box to the display list

  WorldMC.addChild(bx);

  //position and rotate the Box;

  bx.x = Math.random() * -1100;

  bx.y = Math.random() * 1;

  bx.rotation = 40;

  // add the box to list of box

  WorldMC.boxMC.push(bx);

  //trace the length of the array

  trace(WorldMC.boxMC.length);

Views

448

Translate

Translate

Report

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

LEGEND , Mar 31, 2017 Mar 31, 2017

This line:

  if(WorldMC.numChildren<10)

should be:

  while(WorldMC.numChildren<10)

Votes

Translate

Translate
Community Expert ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

your first line of code does nothing but add confusion and your notation is going to add to your confusion.  ie, if you like to use MC to indicate a movieclip, don't use that to indicate an array, too.

anyway, those don't explain the errors.  the first error message would be expected if you have something like:

WorldMC.boxMC=bx

but i don't see that in the code you posted so i suspect you're failing to display enough info to help you.

to remedy, click file>publish settings and tick 'permit debugging'.  retest.

the problematic lines of code will be in the error messages allowing you to pinpoint the problems.  start with the first error and fix it.  then work on the next etc.

Votes

Translate

Translate

Report

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
LEGEND ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

In publishing settings you can check a box that enables debugging. If you do that you'll also get the line number where the problem is happening.

Why do you have the array inside the container movieclip? I would change these lines:

var boxMC:Array = new Array();

// add the box to list of box

  boxMC.push(bx);

  //trace the length of the array

  trace(boxMC.length);

Votes

Translate

Translate

Report

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 Beginner ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

I have 3 classes set up to run my game one for the turrent to shoot my bullet, one for my bullet, and one for my Box

This is my Box class code:

package tankscript.turrent

{

  // import any necessary files/libaries

  import flash.display.MovieClip;

  import flash.events.Event;

  import flash.geom.Point;

  public class Box extends MovieClip

  {

  private var boxSpeed:int;

  private var target:Point;

  function Box()

  {

  // constructor code

  boxSpeed = 10;

  trace("Made a Box!");

  //Set target location of the ship

  target = new Point();

  target.x = Math.random() * 2500;

  target.y = Math.random() * 0;

  // add an event lsitener  to update every frame

  addEventListener(Event.ENTER_FRAME, update);

  }

  function update(e:Event)

  {

  // point the box at the target

  var dx = target.x - x;

  var dy = target.y - y;

  var angle = Math.atan2(dy,dx) / Math.PI * 180;

  rotation = angle;

  // move in direction of the box is facing

  x=x+Math.cos(rotation/180*Math.PI)*boxSpeed;

  y=y+Math.sin(rotation/180*Math.PI)*boxSpeed;

  // Calculate the distance to target

  var hyp = Math.sqrt((dx*dx)+(dy*dy));

  // if the hyp is less than 5 pixels, pick a new target

  if (hyp <5)

  {

  target.x = Math.random() * 2500;

  target.y = Math.random() * 0;

  }

  }

  }

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is my main game code:

~~~~~~

import flash.display.MovieClip;

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.Event;

import flash.display.Stage;

tankMC.gotoAndStop("TankStandStill");

var rightPressed:Boolean = new Boolean(false);

var leftPressed:Boolean = new Boolean(false);

var tankSpeed:Number = 15;

var turrentSpeed:Number = 15;

var boxSpeed:Number = 10;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

stage.addEventListener(Event.ENTER_FRAME, gameLoop);

function keyDownHandler(keyEvent:KeyboardEvent):void

{

  if (keyEvent.keyCode == Keyboard.RIGHT)

  {

  rightPressed = true;

  }

  else if (keyEvent.keyCode == Keyboard.LEFT)

  {

  leftPressed = true;

  }

}

function keyUpHandler(keyEvent:KeyboardEvent):void

{

  if (keyEvent.keyCode == Keyboard.RIGHT)

  {

  rightPressed = false;

  tankMC.gotoAndStop("TankStandStill");

  }

  else if (keyEvent.keyCode == Keyboard.LEFT)

  {

  leftPressed = false;

  tankMC.gotoAndStop("Tank Stand Still Left");

  }

}

function gameLoop(loopEvent:Event):void

{

  //Tank move

  if (rightPressed)

  {

  if (tankMC.x < 1000)

  {

  tankMC.x +=  tankSpeed;

  }

  else if (WorldMC.x > -1050)

  {

  WorldMC.x -=  tankSpeed;

  }

  tankMC.gotoAndStop("Tank MovingRarrow");

  }

  if (leftPressed)

  {

  if (tankMC.x > 700)

  {

  tankMC.x -=  tankSpeed;

  }

  else if (WorldMC.x < 900)

  {

  WorldMC.x +=  tankSpeed;

  }

  tankMC.gotoAndStop("TankMovingLeft");

  }

  //Turrent move

  if (rightPressed)

  {

  if (turrentMC.x < 940)

  {

  turrentMC.x +=  turrentSpeed;

  }

  else if (WorldMC.x > -1050)

  {

  WorldMC.x -=  turrentSpeed;

  }

  }

  if (leftPressed)

  {

  if (turrentMC.x > 640)

  {

  turrentMC.x -=  turrentSpeed;

  }

  else if (WorldMC.x < 900)

  {

  WorldMC.x +=  turrentSpeed;

  }

  }

  var boxMC:Array;

  WorldMC.boxMC = new Array();

  // Variables etc

  //------------------------------

  // -----Box Spawning------------

  //------------------------------

  //only spawn a box if there are less tan 100 already on screen

  if(WorldMC.numChildren<10)

  {

  // Make a new instance of the box class

  var bx = new Box();

  // add the box to the display list

  WorldMC.addChild(bx);

  //position and rotate the Box;

  bx.x = Math.random() * -1100;

  bx.y = Math.random() * 1;

  bx.rotation = 40;

  // add the box to list of box

  WorldMC.boxMC.push(bx);

  //trace the length of the array

  trace(WorldMC.boxMC.length);

  }

}

Votes

Translate

Translate

Report

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 ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

what's the point of posting all that?

did you read either response to your first post?

Votes

Translate

Translate

Report

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 Beginner ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

var boxMC:Array;

  boxMC = new Array();

  // Variables etc

  //------------------------------

  // -----Box Spawning------------

  //------------------------------

  //only spawn a box if there are less tan 100 already on screen

  if(WorldMC.numChildren<10)

  {

  // Make a new instance of the box class

  var bx = new Box();

  // add the box to the display list

  WorldMC.addChild(bx);

  //position and rotate the Box;

  bx.x = Math.random() * -1100;

  bx.y = Math.random() * 1;

  bx.rotation = 40;

  // add the box to list of box

  boxMC.push(bx);

  //trace the length of the array

  trace(boxMC.length);

  }

}

i fixed it and now it says:

1180: Call to a possibly undefined method Box.

"Box" is my class

Votes

Translate

Translate

Report

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 ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

import your Box class.

Votes

Translate

Translate

Report

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 Beginner ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

Would you have an idea on my its only tracing 1 boxMC instead of the 10 im trying to count? it says "Made a Box! 1 Made a Box! 1" and and over until it hits 10 times. im trying to get the number 1 to go up to count the 10 boxMC im trying to count

Votes

Translate

Translate

Report

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 Beginner ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

on why its only*

Votes

Translate

Translate

Report

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
LEGEND ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

This line:

  if(WorldMC.numChildren<10)

should be:

  while(WorldMC.numChildren<10)

Votes

Translate

Translate

Report

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 Beginner ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

Oh that worked! Thank you very much

Votes

Translate

Translate

Report

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
LEGEND ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

"Turret".

Not "turrent".

Votes

Translate

Translate

Report

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
LEGEND ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

I was going to comment on that, but it wouldn't break the code, unless the actual folder is named 'turret'.

Votes

Translate

Translate

Report

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
LEGEND ,
Mar 31, 2017 Mar 31, 2017

Copy link to clipboard

Copied

While the code doesn't care, there's a chance it would have ended up in UI text like that too, given the consistent nature of the misspelling.

Votes

Translate

Translate

Report

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 Beginner ,
Apr 03, 2017 Apr 03, 2017

Copy link to clipboard

Copied

I noticed my misspelling of that word a bit after i started my coding and decided to just play it safe and not to to go about and fix the wording

Votes

Translate

Translate

Report

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 Beginner ,
Apr 04, 2017 Apr 04, 2017

Copy link to clipboard

Copied

I have a different class for making my bullet shoot out of my turrent. But i need to make my bullet spawn on to my "WorldMC". Like i have the box spawning on it. but since i have my code in a class i cant seem to do "WorldMC.addChild(b)" i get an error saying "Access of undefined property WorldMC" do i need to import something? if so how would that code go?

package tankscript.turrent

{

  import flash.display.MovieClip;

  import flash.events.Event;

  import flash.events.MouseEvent;

  import flash.display.Stage;

  public class Turrent extends MovieClip

  {

  var shotCooldown:int;

  const MAX_COOLDOWN = 10;

  public function Turrent()

  {

  // constructor code

  // set the shot cooldown

  shotCooldown = MAX_COOLDOWN;

  // add an event listener to update the turrent every frame

  addEventListener(Event.ENTER_FRAME, update);

  // set up an event listener for when the turrent is added to the stage

  addEventListener(Event.ADDED_TO_STAGE, initialise);

  }

  function initialise(e:Event)

  {

  // add an click Listener to the stage

  stage.addEventListener(MouseEvent.CLICK, fire);

  }

  function fire(m:MouseEvent)

  {

  trace("Fired!");

  //if we're allowed to shoot

  if (shotCooldown<=0)

  {

  // reset the cooldown

  shotCooldown = MAX_COOLDOWN;

  //spawn a bullet

  var b = new Bullet();

  // set the position and roation of the bullet

  b.rotation = rotation;

  b.x = x;

  b.y = y;

  parent.addChild(b);

  // play firing animation

  play();

  }

  }

  function update(e:Event)

  {

  // reduce the shot cooldown by 1

  shotCooldown--;

  //Make the turrent face the mouse

  if (parent != null)

  {

  var dx = parent.mouseX - x;

  var dy = parent.mouseY - y;

  var angle = (Math.atan2(dy,dx)/Math.PI*180);

  rotation = angle;

  }

  }

  }

}

Votes

Translate

Translate

Report

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
LEGEND ,
Apr 06, 2017 Apr 06, 2017

Copy link to clipboard

Copied

Your code doesn't include WorldMC.addChild(b), you're using parent.addChild(b), which ought to work.

Are you getting any errors, or bullets not appearing?

Votes

Translate

Translate

Report

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 Beginner ,
Apr 06, 2017 Apr 06, 2017

Copy link to clipboard

Copied

How everything is at the moment it works, but i'm about to put a hit test object in my code but the code wont work if my boxs and bullets are on two different areas. Unless i'm mistaken

Votes

Translate

Translate

Report

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
LEGEND ,
Apr 06, 2017 Apr 06, 2017

Copy link to clipboard

Copied

There is hitTestPoint(), where you could test any point value to see if it hit an object. Here's a discussion about whether to use hittestobject or hittestpoint for the case of bullets:

actionscript 3 - AS3: hitTestObject vs hitTestPoint for Bullet vs Enemy - Stack Overflow

Votes

Translate

Translate

Report

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 Beginner ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

LATEST

I would use it, but that is at a point my boxes are moving. Im not sure how well it would work

Votes

Translate

Translate

Report

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