AS3 Question(s)

Need programming help? Want to post programming tips? Are you programming a game and want to show it off? That and more here.

AS3 Question(s)

Postby Blablob » December 9th, 2012, 6:50 am

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.)
User avatar
Blablob
Content for Contempt

 
Posts: 3108
Joined: September 6th, 2009, 3:38 pm
Location: That way

Credit To Team

Thumbs Up given: 67 times
Thumbs Up received: 229 times

Re: AS3 Question(s)

Postby Suyo » December 9th, 2012, 7:22 am

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;
         }
Image

Image

Image

Image

Image

Image
User avatar
Suyo
"quite easily the most manly man of all" --Raz

Error contacting Twitter
Error contacting last.fm
 
Posts: 2771
Joined: July 28th, 2009, 2:41 am
Location: Nuremberg (Germany)

Runouw Votes Winner
For winning the RV New Year 2012 Award for Best Moderator. Like you didn't know. XD

Thumbs Up given: 26 times
Thumbs Up received: 359 times

Re: AS3 Question(s)

Postby Blablob » December 9th, 2012, 7:40 am

No, that didn't work.
User avatar
Blablob
Content for Contempt

 
Posts: 3108
Joined: September 6th, 2009, 3:38 pm
Location: That way

Credit To Team

Thumbs Up given: 67 times
Thumbs Up received: 229 times

Re: AS3 Question(s)

Postby Buff_ » December 9th, 2012, 7:41 am

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.
User avatar
Buff_
A rather stellar gentleman

Error contacting Twitter
 
Posts: 2827
Joined: August 7th, 2009, 6:48 am
Location: Under the sink

The Start of Something Big

Thumbs Up given: 10 times
Thumbs Up received: 104 times

Re: AS3 Question(s)

Postby Blablob » December 9th, 2012, 8:00 am

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
User avatar
Blablob
Content for Contempt

 
Posts: 3108
Joined: September 6th, 2009, 3:38 pm
Location: That way

Credit To Team

Thumbs Up given: 67 times
Thumbs Up received: 229 times

Re: AS3 Question(s)

Postby Buff_ » December 9th, 2012, 9:04 am

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.
User avatar
Buff_
A rather stellar gentleman

Error contacting Twitter
 
Posts: 2827
Joined: August 7th, 2009, 6:48 am
Location: Under the sink

The Start of Something Big

Thumbs Up given: 10 times
Thumbs Up received: 104 times

Re: AS3 Question(s)

Postby Blablob » December 11th, 2012, 3:57 pm

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.
User avatar
Blablob
Content for Contempt

 
Posts: 3108
Joined: September 6th, 2009, 3:38 pm
Location: That way

Credit To Team

Thumbs Up given: 67 times
Thumbs Up received: 229 times

Re: AS3 Question(s)

Postby Buff_ » December 11th, 2012, 4:23 pm

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.
User avatar
Buff_
A rather stellar gentleman

Error contacting Twitter
 
Posts: 2827
Joined: August 7th, 2009, 6:48 am
Location: Under the sink

The Start of Something Big

Thumbs Up given: 10 times
Thumbs Up received: 104 times

Re: AS3 Question(s)

Postby Blablob » December 12th, 2012, 3:58 pm

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.
User avatar
Blablob
Content for Contempt

 
Posts: 3108
Joined: September 6th, 2009, 3:38 pm
Location: That way

Credit To Team

Thumbs Up given: 67 times
Thumbs Up received: 229 times

Re: AS3 Question(s)

Postby Jellonator » December 13th, 2012, 5:44 am

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.
bepis lmao
User avatar
Jellonator
Code: Awesome

Error contacting Twitter
 
Posts: 342
Joined: August 28th, 2010, 6:25 am
Location: With Waldo.

Thumbs Up given: 18 times
Thumbs Up received: 42 times

Next

Return to Programming