3

I'm fairly new to AS3, and I've been trying to make a side scrolling shooter. I made some progress but I've hit a wall on the bullets themselves. The code I've been using is:

var circle:Sprite = new Sprite();

function shoot() {
  circle.graphics.beginFill(0xFF794B);
  circle.graphics.drawCircle(0, 00, 7.5);
  circle.graphics.endFill();
  addChild(circle);
  circle.x = ship_mc.x;
  circle.y = ship_mc.y + 43; 
}

The problem with this is it only allows for one bullet on the screen at a time. How can I change this so the bullets are created so that I can have an unlimited amount of them?

1
  • Please don't forget to accept an answer. And, when you have more reputation, upvote one or more answers. Commented Feb 2, 2013 at 0:04

2 Answers 2

2

Create the object inside the method

function shoot() {
    var circle:Sprite = new Sprite();
    circle.graphics.beginFill(0xFF794B);
    circle.graphics.drawCircle(0, 00, 7.5);
    circle.graphics.endFill();
    addChild(circle);
    circle.x = ship_mc.x;
    circle.y = ship_mc.y + 43; 
}

Otherwise, you would only have one circle variable. This time, a new circle is created each time the method is called.

However, you will probably want to store all your circles somehow so that you can remove them later.

var allCircles: Vector.<Sprite> = new Vector.<Sprite>();
function shoot() {
    var circle:Sprite = new Sprite();
    circle.graphics.beginFill(0xFF794B);
    circle.graphics.drawCircle(0, 00, 7.5);
    circle.graphics.endFill();
    addChild(circle);
    circle.x = ship_mc.x;
    circle.y = ship_mc.y + 43; 
    allCircles.push(circle);
}

Then, at a later time, you can loop through all your circles:

for each (var circle: Sprite in allCircles) {
    // do something with this circle
}

And to clear all circles:

for each (var circle: Sprite in allCircles) {
    removeChild(circle);
}
allCircles.clear();
Sign up to request clarification or add additional context in comments.

2 Comments

But then he wouldn't have any way of interacting with the circles without accessing them through the stage's children.
There was no mentioning about removing them in the original question :) But you are correct.
0

You want to store an array of Sprites, not just one of them. First you declare your Array:

var circles:Array = new Array(); 

Then you change your shoot function to make a new one and then push it into the Array:

function shoot() {
    var circle:Sprite = new Sprite();

    circle.graphics.beginFill(0xFF794B);
    circle.graphics.drawCircle(0, 00, 7.5);
    circle.graphics.endFill();
    circle.x = ship_mc.x;
    circle.y = ship_mc.y + 43; 

    circles.push(circle);  
    addChild(circles[circles.length-1]);
}

1 Comment

I'd prefer Vector ahead of Array. Also, why not addChild(circle);? why complicate things by taking the last index of the array? But the idea of an array/vector is a good one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.