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

Copy link to clipboard

Copied

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

Views

300

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

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;

}

Votes

Translate

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

Copy link to clipboard

Copied

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;

}

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

Copy link to clipboard

Copied

Thank you very much!!!

It works perfect!

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

Copy link to clipboard

Copied

LATEST

you're welcome.

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