Page 1 of 3

AS3 Question(s)

PostPosted: December 9th, 2012, 6:50 am
by Blablob
Hi all. Recently I've been working with the FlashDevelop IDE, and I've been working on a simple text-based project while simulataneously messing around with code to see what I can do.

Anyways, today I was trying to see if I could create an object-spawner where objects would spawn when I tell them to. Unfortunately I haven't had much success with this. Here is the code I have been using:

var shape:Sprite = new Sprite();
var shape2:Sprite = new Sprite();

shape.graphics.lineStyle(1, 0x00ff00);
shape.graphics.beginFill(0xFFFA24);
shape.graphics.drawRect(0, 0, 10, 10);
shape.graphics.endFill();
shape.x = mouseX;
shape.y = mouseY;
addChild(shape);

shape2.graphics.lineStyle(1, 0x00ff00);
shape2.graphics.beginFill(0xFFFA24);
shape2.graphics.drawRect(0, 0, 10, 10);
shape2.graphics.endFill();
shape2.x = shape.x;
shape2.y = shape.y;


addEventListener(Event.ENTER_FRAME, follow);
function follow(e:Event):void
{
shape.x = mouseX;
shape.y = mouseY;
addChild(shape2);
}


Basically I'm trying to tell the code that an object ("shape") is following my mouse. That works fine. But I'm also trying to say that another object ("shape2") will spawn on the first object on every frame. The problem is that this only works on the first frame, then "shape2" will stop spawning. Anybody know how to fix this?

(I may refer to this topic in the future if/when I have other AS3 Questions. Alternatively other users can use this topic to ask their AS questions, because as far as I know we don't have an official topic.)

Re: AS3 Question(s)

PostPosted: December 9th, 2012, 7:22 am
by Suyo
I don't use the default AS3 framework so I have no idea how Sprite works, but try switch around parts of shape2:

Code: Select all
         shape2.graphics.lineStyle(1, 0x00ff00);
         shape2.graphics.beginFill(0xFFFA24);
         shape2.graphics.drawRect(0, 0, 10, 10);
         shape2.graphics.endFill();
         addChild(shape2);
         
         
         addEventListener(Event.ENTER_FRAME, follow);
         function follow(e:Event):void
         {
            shape.x = mouseX;
            shape.y = mouseY;
            
            shape2.x = shape.x;
            shape2.y = shape.y;
         }

Re: AS3 Question(s)

PostPosted: December 9th, 2012, 7:40 am
by Blablob
No, that didn't work.

Re: AS3 Question(s)

PostPosted: December 9th, 2012, 7:41 am
by Buff_
From what I can tell, it's because you're spawning the same object in the same place at every frame. Once you've assigned the shape2 x and y values, you never change them as it only ever gets called once. Suyo's code may work.. but that the shape would probably only appear once and at (0,0). Moving the addChild(shape2) into the follow function should fix that.

Re: AS3 Question(s)

PostPosted: December 9th, 2012, 8:00 am
by Blablob
Buffooner wrote:From what I can tell, it's because you're spawning the same object in the same place at every frame. Once you've assigned the shape2 x and y values, you never change them as it only ever gets called once. Suyo's code may work.. but that the shape would probably only appear once and at (0,0). Moving the addChild(shape2) into the follow function should fix that.


Here's what the code looks like right now:

var shape:Sprite = new Sprite();
var shape2:Sprite = new Sprite();

shape.graphics.lineStyle(1, 0x00ff00);
shape.graphics.beginFill(0xFFFA24);
shape.graphics.drawRect(0, 0, 10, 10);
shape.graphics.endFill();
shape.x = mouseX;
shape.y = mouseY;
addChild(shape);

shape2.graphics.lineStyle(1, 0x00ff00);
shape2.graphics.beginFill(0xFFFA24);
shape2.graphics.drawRect(0, 0, 10, 10);
shape2.graphics.endFill();


addEventListener(Event.ENTER_FRAME, follow);
function follow(e:Event):void
{
addChild(shape2);

shape.x = mouseX;
shape.y = mouseY;

shape2.x = shape.x;
shape2.y = shape.y;

trace("Shape: " + shape.x + ", " + shape.y);
trace("Shape2: " + shape2.x + ", " + shape2.y);
}


I added "trace" just so I could track where my objects were. And both objects follow the mouse now.
Just to clarify, what I was trying to do is figure out how to spawn MULTIPLE instances of shape2 that would never move but would always spawn at the first shape's location.

I meant to provide an example in the OP but it must have slipped my mind. Here's what I'm talking about: http://www.newgrounds.com/portal/view/376869

Re: AS3 Question(s)

PostPosted: December 9th, 2012, 9:04 am
by Buff_
I haven't used the FlashDevelop IDE but, in Flash itself, this works.

Code wrote:package {
import flash.events.Event;
import flash.display.Sprite;

public class Main extends Sprite {

private var shape:Sprite = new Sprite();
private var shape2:Sprite = new Sprite();

public function Main() {
shape.graphics.lineStyle(1, 0x000000);
shape.graphics.beginFill(0x000000);
shape.graphics.drawRect(0,0,50,50)
shape.graphics.endFill();
shape.x=mouseX;
shape.y=mouseY;
addChild(shape);

addEventListener(Event.ENTER_FRAME, follow);
}

private function follow(e:Event):void {
shape.x=mouseX;
shape.y=mouseY;
createShape2();
trace("Shape: " + shape.x + ", " + shape.y);
trace("Shape2: " + shape2.x + ", " + shape2.y);
}

private function createShape2() {
shape2 = new Sprite();
addChild(shape2);
shape2.graphics.lineStyle(2, 0xAAADDD);
shape2.graphics.beginFill(0xFFFFFF);
shape2.graphics.drawRect(0, 0, 10, 10);
shape2.graphics.endFill();
shape2.x=shape.x;
shape2.y=shape.y;
}
}
}


or in code tags

Code: Select all
import flash.events.Event;
   import flash.display.Sprite;
   
   public class Main extends Sprite {

      private var shape:Sprite = new Sprite();
      private var shape2:Sprite = new Sprite();

      public function Main() {
         shape.graphics.lineStyle(1, 0x000000);
         shape.graphics.beginFill(0x000000);
         shape.graphics.drawRect(0,0,50,50)
         shape.graphics.endFill();
         shape.x=mouseX;
         shape.y=mouseY;
         addChild(shape);
   
         addEventListener(Event.ENTER_FRAME, follow);
      }

      private function follow(e:Event):void {
         shape.x=mouseX;
         shape.y=mouseY;
         createShape2();
         trace("Shape: " + shape.x + ", " + shape.y);
         trace("Shape2: " + shape2.x + ", " + shape2.y);
      }
      
      private function createShape2() {
         shape2 = new Sprite();
         addChild(shape2);
         shape2.graphics.lineStyle(2, 0xAAADDD);
         shape2.graphics.beginFill(0xFFFFFF);
         shape2.graphics.drawRect(0, 0, 10, 10);
         shape2.graphics.endFill();
         shape2.x=shape.x;
         shape2.y=shape.y;
      }
   }
}


The extra stuff at the top is probably self included by FlashDevelop so you may need to ignore that. I would also recommend you move the first shape creation to it's own function.

Sorry about the differences and my (very) wrong explanation above. The differences were for me to check where your squares were, and I knew what I was trying to say but.. I said it wrong.

Re: AS3 Question(s)

PostPosted: December 11th, 2012, 3:57 pm
by Blablob
Oh hi, I'm back with another problem.

I think I keep running into the same issue over and over again and I haven't figured out why. Right now I'm trying to design a shooter by having the character move left and right, and pressing space to shoot. But I can only seem to fire one bullet at a time. Here's what I'm using: http://www.newgrounds.com/dump/item/aca ... cb71f9089b

And here's the code:

code: show
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.events.KeyboardEvent;

public class Main extends Sprite
{
private var ship:Sprite = new Sprite();
private var bullet:Sprite = new Sprite();

public function Main()
{

//Make the Ship and Bullet Graphic

ship.graphics.beginFill(0x32B6CD, 1);
ship.graphics.lineStyle(2, 0x000000, 1);
ship.graphics.drawRect(-10, 0, 20, 50);
ship.x = 400;
ship.y = 600 - (ship.height + 10);

bullet.graphics.beginFill(0xFEF916, 1);
bullet.graphics.lineStyle(2, 0x000000, 1);
bullet.graphics.drawRect( -1, 0, 2, 10);
bullet.graphics.endFill();

addChild(ship);

//Listen for stuff

stage.addEventListener(KeyboardEvent.KEY_DOWN, traceKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, left);
stage.addEventListener(KeyboardEvent.KEY_DOWN, right);
stage.addEventListener(KeyboardEvent.KEY_DOWN, shoot);
}

//Check key codes

public function traceKey(e:KeyboardEvent):void
{
if (e.keyCode != 37 && e.keyCode !=39 && e.keyCode !=32)
trace(e.keyCode);
}

//Left movement

public function left(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
ship.x -= 10;
trace("Left!");
}

}

//Right movement

public function right(e:KeyboardEvent):void
{
if (e.keyCode == 39)
{
ship.x += 10;
trace("Right!");
}
}

//Shoot bullet

public function shoot(e:KeyboardEvent):void
{
if (e.keyCode == 32)
{
bullet.x = ship.x;
bullet.y = ship.y;
addChild(bullet);
addEventListener(Event.ENTER_FRAME, bulletMove);
trace("Shoot!");
}
}

private function bulletMove(e:Event):void
{
bullet.y -= 15;
}
}
}


I'll take a wild guess and say the problem is very similar to what I had before, lol. But any help and explanation of what I'm doing wrong would be greatly appreciated.

Re: AS3 Question(s)

PostPosted: December 11th, 2012, 4:23 pm
by Buff_
Yeah, it's pretty much the same as you had before. You're only creating bullet once, which is at the start of the code. When you shoot you need to create another instance of the bullet which is defined by: Var bullet=new Sprite();. Your problem here however is that once you've created that new bullet, the other one(s) will stop working as the other one is called bullet as well. I'm on my phone right now, so I can't help you fully on this, but bullet will need to be an array at some point.

And just as a general note, you have a load of separate functions for things that can be grouped together. For example, your controls should be in one function since they're similar and you pass in the same parameter. You can do an if, but it's cleaner to do a switch statement. These compare values to the same variable. I'd advise looking them up.

That's it for now though. I hope I helped somewhat, but I should be able to give much more information tomorrow.

Re: AS3 Question(s)

PostPosted: December 12th, 2012, 3:58 pm
by Blablob
Thanks Buff. But I looked up a tutorial on the Switch statement and I can't quite figure out how to use it. This is what mine looks like:

public function moveAndShoot(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case "37":
ship.x -= 10;
trace("Left!");
break;

case "39":
ship.x += 10;
trace("Right!");
break;

case "32":
bullet.x = ship.x;
bullet.y = ship.y;
addChild(bullet);
trace("Shoot!");
break;
}

}


What am I doing wrong?
Also I'll mess around with the array I created later, but if you're still planning to give me more information I'd greatly appreciate that.

Re: AS3 Question(s)

PostPosted: December 13th, 2012, 5:44 am
by Jellonator
You are using switch correctly, but you only have one instance of 'bullet,' which you are reusing over and over again. Try using arrays, which should allow you to easily add and remove bullets. Either that or AS3's Vector class.