Dynamically creating objects in actionscript 3? - actionscript-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?

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();

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]);
}

Related

detect mask fill as3

how to detect if my mask is filled about 90%? here is my drawing code.
var my_shape:Sprite = new Sprite();
var bitmapData:BitmapData = new BitmapData(shida.width, shida.height);
bitmapData.draw(shida);
shidaq.addEventListener(MouseEvent.MOUSE_MOVE, draw);
function draw(e:MouseEvent):void {
addChild(my_shape);
my_shape.mask = shida;
my_shape.graphics.beginFill(0xA7A9AC,1);
my_shape.graphics.drawCircle(mouseX, mouseY, 40);
my_shape.graphics.endFill();
}

Remove sprite from stage in as3

I have this code for a message to appear on stage when player finishes drag and drop. I would like this sprite to be removed when a button is clicked for the next frame. Can someone help me with the code?
stage.addEventListener(Event.ENTER_FRAME, EntFrame);
function EntFrame (e:Event):void
{
if (CntP1+CntP2+CntP3+CntP4+CntP5+CntP6+CntP7+CntP8 == 40)
{
var w:int = 400, h:int = 200;
var win:Sprite = new Sprite();
win.name = "Mywin";
addChild(win);
// draw rounded rect with subtle vertical linear gradient fill and blue stroke
win.graphics.lineStyle(4,0x0077ff);
var mat:Matrix = new Matrix();
mat.createGradientBox(w, h, 90 * (Math.PI / 180));
win.graphics.beginGradientFill(GradientType.LINEAR,[0xffffff,0xeeeeee],[1.00,1.00],[0,255],mat);
win.graphics.drawRoundRect(0,0,w,h,15,15);
// show center "YOU WIN!" text
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.antiAliasType = AntiAliasType.ADVANCED;
tf.defaultTextFormat = new TextFormat("Arial, Verdana",36,0x454545,true);
tf.text = "Κέρδισες!";
tf.selectable = false;
win.addChild(tf);
tf.x = w/2 - tf.width/2;
tf.y = h/2 - tf.height/2;
// add a drop shadow
var dropShadow:DropShadowFilter = new DropShadowFilter(3,45,0,.35,8,8,1,3);
win.filters = [dropShadow];
// center the graphic
win.x = stage.stageWidth/2 - win.width/2;
win.y = stage.stageHeight/2 - win.height/2;
}
}
Your code isn't written well and needs rewriting to ensure reuse or scalability of your project, but here's a quick way out.
make a holder Sprite, something like
var messageHolder:Sprite = new Sprite();
addChild(messageHolder);
add all the messages to that holder in any fashion you like. When you need to erase the contents of that holder, call following method:
function clearHolderContents(holder:DisplayObjectContainer):void
{
if (holder.numChildren < 1)
return; // no need to continue this method if the target is empty
for (var i:int = holder.numChildren - 1; i >= 0; i--)
removeChild(holder.getChildAt(i));
}
This method can clear contents of any DisplayObjectContainer => use it for your messageHolder:
clearHolderContents(messageHolder);
Hope that helps!

Drawing triangle shape background

I have three Points in the array. I want to draw a triangle using this Points.
For now, I achieve to draw a border of this triangle using MoveTo() and LineTo() functions.
The problem is, I also need to draw an inner background of an area that this lines creates.
Is there a way to achieve this?
You should use graphics.beginFill(color);:
public function astest()
{
var verticies:Vector.<Point> = Vector.<Point>([new Point(0, 100), new Point(100, 0), new Point(100, 100)]);
var sh:Shape = new Shape();
addChild(sh);
drawPolygon(sh.graphics, verticies, 0xFF0000);
}
protected function drawPolygon(graphics:Graphics, verticies:Vector.<Point>, color:uint):void
{
graphics.beginFill(color);
var p:Point = verticies.shift();
graphics.moveTo(p.x, p.y);
for(var i:int = 0; i < verticies.length; i++)
{
p = verticies[i];
graphics.lineTo(p.x, p.y);
}
graphics.endFill();
}

How to beginBitmapFill without repeats?(AS3)

my code:
myCircle = new Shape();
function doStuffWithBitmapData(bmd:BitmapData):void
{
myCircle = new Shape();
var matrix:Matrix = new Matrix();
matrix.translate(0, 0);
myCircle.graphics.beginBitmapFill(bmd, matrix, false);
myCircle.graphics.drawCircle(0, 0, 17);
myCircle.graphics.endFill();
myCircle.x = 40;
myCircle.y = 63;
addChild(myCircle);
// your code
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event)
{
myCircle.rotation += 3;
}
I need to fill the circle with image , but the image is repeating many times, but if I set the repeat to false, the picture will be bigger, can I make no repeat and at the same time don't change the sizes of the filled image?
i'm not too sure of the the bitmapfill method, but creating your own bitmap and bitmapData, and then using your image's bitmapdata, you can manipulate the image pixel/data anyway you like.
-Using the setPixel/setPixel32 method of the bitmapData class will be helpful in your task (google is your friend)
Adobe's help: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html
Please look closely at the draw() method of BitmapData Class Documents. It is very important.
and refer a following code.
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Bitmap;
var myCircle:Shape;
var bmd:BitmapData = new BitmapData(600,400,false,0xffffff);
var bmp:Bitmap = new Bitmap(bmd);
this.addChild(bmp);
var circleBitmapData:BitmapData = new BitmapData(20,20,false,0xffffff * Math.random());
myCircle = new Shape();
var matrix:Matrix = new Matrix();
myCircle.graphics.beginBitmapFill(circleBitmapData);
myCircle.graphics.drawCircle(0, 0, 20);
myCircle.graphics.endFill();
myCircle.x = 40;
myCircle.y = 63;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event)
{
bmd.draw(myCircle, myCircle.transform.matrix, myCircle.transform.colorTransform);
myCircle.x = Math.random() * stage.width;
myCircle.y = Math.random() * stage.height;
}

Drawing point in flash

I try to draw some points by coding. But the 1px width point seems has feature at the edge.
Even I try to draw some position ten times.
This is my code:
private function createPoint(radio:Number, rep:int):Shape{
var s:Shape = new Shape();
var i:int = rep;
while( i-- > 0 ){
s.graphics.beginFill(0, 1);
s.graphics.drawRect(0,0, radio, radio);
s.graphics.endFill();
}
return s;
}
private function drawPoint():void
{
this.addChild( createPoint(10) );
}
// param must be >= 1
private function createPoint(radio:uint):Shape
{
var s:Shape = new Shape();
s.graphics.beginFill(0x00ff00, 1);
s.graphics.drawRect(0,0, radio, radio);
s.graphics.endFill();
return s;
}
Will certainly work. You may not of been adding the returned shape into the display list.