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

Fire multiple bullets but when i put the looping i get error (access of undefined property shot)

Community Beginner ,
Feb 05, 2016 Feb 05, 2016

stage.addEventListener(Event.ENTER_FRAME,looping);

function looping (event:Event):void

{

  shot.x +=10;

}

fire_btn.addEventListener(MouseEvent.CLICK,fire);

function fire(MouseEvent:Event):void

{

  var shot:bullet = new bullet();

  this.addChild(shot);

  shot.x = player.x;

  shot.y = player.y;

}

TOPICS
ActionScript
366
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 , Feb 05, 2016 Feb 05, 2016

you're making shot local to fire() and losing your reference.

use:

var shotA:Array=[];

stage.addEventListener(Event.ENTER_FRAME,looping);

function looping (event:Event):void

{

for(var i:int=shotA.length-1;i>=0;i--){

  shotA.x +=10;

if(shotA.x>stage.stageWidth){

shotA.splice(i,1);

}

}

}

fire_btn.addEventListener(MouseEvent.CLICK,fire);

function fire(MouseEvent:Event):void

{

  var shot:bullet = new bullet();

shotA.push(shot);

  this.addChild(shot);

  shot.x = player.x;

  shot.y = player.y;

}

Translate
Community Expert ,
Feb 05, 2016 Feb 05, 2016

you're making shot local to fire() and losing your reference.

use:

var shotA:Array=[];

stage.addEventListener(Event.ENTER_FRAME,looping);

function looping (event:Event):void

{

for(var i:int=shotA.length-1;i>=0;i--){

  shotA.x +=10;

if(shotA.x>stage.stageWidth){

shotA.splice(i,1);

}

}

}

fire_btn.addEventListener(MouseEvent.CLICK,fire);

function fire(MouseEvent:Event):void

{

  var shot:bullet = new bullet();

shotA.push(shot);

  this.addChild(shot);

  shot.x = player.x;

  shot.y = player.y;

}

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 Beginner ,
Feb 06, 2016 Feb 06, 2016

Thank you very much!!!

It works perfect!

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 ,
Feb 06, 2016 Feb 06, 2016
LATEST

you're welcome.

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