Skip to main content
Known Participant
February 5, 2016
Answered

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

  • February 5, 2016
  • 1 reply
  • 405 views

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;

}

This topic has been closed for replies.
Correct answer kglad

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;

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 5, 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;

}

Known Participant
February 6, 2016

Thank you very much!!!

It works perfect!

kglad
Community Expert
Community Expert
February 6, 2016

you're welcome.