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.

Re: AS3 Question(s)

Thumbs up x1

Postby Buff_ » December 13th, 2012, 10:37 am

Blablob wrote: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.


Sorry, but the way I was going to explain it won't work with your code (mine was with movieclips because that's the only way I've done it so far. I'm still a beginner at AS3 too :D). At the minute I'm juggling work with helping you so I haven't always got time to spend on playing with your code and seeing if I can get it working.

Switch is like an if, but you're comparing multiple values against one variable. The case is each value, and break separates them so that it doesn't continue to do the code after you've pressed one value.

Code: Select all
switch (e.keyCode) {
            case 37://left
               ship.x-=10;
               break;
            case 38://up
               ship.y-=10;
               break;
            case 39://right
               ship.x+=10;
               break;
            case 40://down
               ship.y+=10;
               break;
         }


is the same as

Code: Select all
if (e.keyCode == 37) {
         ship.x-=10;
}
         
if(e.keyCode == 38) {
   ship.y-=10;
}

if(e.keyCode==39) {
   ship.x+=10;
}
         
if(e.keyCode==40) {
   ship.y+=10;
}


Hope that clears up switch statements. I'll work on seeing if I can get your code working.
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 15th, 2012, 7:14 am

Can you explain to me how to use an array properly? I've tried a lot of things and I've looked up a few tutorials but I haven't gotten anywhere.
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 15th, 2012, 1:42 pm

An array is basically a list in the same variable name (you can find a much more in depth definition elsewhere).

In Actionscript 3, an array is defined by var [name]:Array, and can be actively made to be used by var [name] = new Array() if it has already been defined. Unlike some other languages you don't have to set the number of items the array can hold, but rather it adds at runtime. Each specific item can be accessed by [name] followed by [num]. So for example, an array called thisArray could have two items in it which can be accessed by using thisArray[0] and thisArray[1]. The numbers go from 0 onwards because computers count on from 0.




or read from adobe's site: http://help.adobe.com/en_US/FlashPlatfo ... Array.html
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 15th, 2012, 2:09 pm

Oh, I should have been more specific. I know what an array can do, but I can't figure out how to use it to create a shoot function. And I assume I need to use the [name].push function for that, but I have no idea how.
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)

Thumbs up x1

Postby Buff_ » December 15th, 2012, 4:15 pm

Orite. The [name].push function adds a value to the end of that variable. So if you do [name][1].push(2) and the value is already 3, it should become 3, 2.

For multiple bullets I would suggest looking up the forEach function. It's inbuilt so it handles things for you, but you will still have to write a function with it.




And I am still planning on helping you with your code. Last week before hols here, so things are a bit hectic.
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 22nd, 2012, 5:37 am

Well, Buff, whenever you're ready to help me with my code is fine by me. I'm on Holidays now too, so I have a lot of time to mess around with code. I'm hoping to get a little more done than just learn how to shoot bullets. :>
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 Blablob » June 5th, 2013, 3:36 am

Okay, so I'm back after a six month hiatus. I haven't coded anything in a few weeks, so I'm a little rusty. Anyways, I'm working on a shooter again (just a small project - I expect to be done by the weekend), but I ran into a bit of a wall again with the shooting. This time I'm trying to use an array to hold all my bullets, but when I try to remove these bullets, I get an error message.

Here is my function which creates and shoots the bullet. For simplicity's sake, I set the boundary to 250 so I could check whether the bullets actually disappear or not.

private function shoot():void
{

var bullet:playerBullet = new playerBullet;
bullet.x = playerShip.x-20;
bullet.y = playerShip.y+40;
container.addChild(bullet);
bulletArray.push(bullet);
bulletCounter = 12;
shootSound.play();

addEventListener(Event.ENTER_FRAME, moveBullet);

function moveBullet(e:Event):void
{
bullet.x += playerBulletSpeed;

if (bulletArray[0].x > 250)
{
container.removeChild(bulletArray[0]);
}
}

}


In case you weren't sure, bulletCounter is what I use to keep the player from shooting.

So, when the first bullet approaches 250x, Flash gives me this error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-8()


Here's a link to the game if you think it will help. You'll notice the first bullet disappears but the rest pass through the boundary.
http://www.newgrounds.com/dump/item/d31 ... 9cc4605b0b
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_ » June 5th, 2013, 5:22 am

You're calling the function to remove the bullet every frame, so that even after it's been removed the value is still beyond 250 and will try to remove a non-existant object.
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 » June 5th, 2013, 7:25 am

I added a removeEventListener to remove the function (I brainfarted and forgot to do that), but it still didn't work. Luckily, I was able to scrape something together to fix the problem. Here's what I did:

function moveBullet(e:Event):void
{
bullet.x += playerBulletSpeed;

if (bullet.x > 250)
{
bulletArray.pop();
container.removeChild(bullet);
removeEventListener(Event.ENTER_FRAME, moveBullet);
}
}
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)

Thumbs up x2

Postby Runouw » June 5th, 2013, 2:48 pm

I still see a problem with how you are removing bullets. It may work for now but you will run into a serious problem later. The problem is for every bullet bullet you spawn it pushes it to the end of the array and for every time a bullet is removed it pops from the end. This is a problem because you are removing the wrong bullet from that array.

A small fix would be the following:
Instead of
Code: Select all
bulletArray.pop();

You could use
Code: Select all
for(var i:int=0;i<bulletArray.length;i++){
  if(bulletArray[i] == bullet){
    bulletArray.splice(i, 1);
    break;
  }
}

Because this new code will guarantee you remove the right bullet from the array. In the future you don't know what order the bullets will be removed in. Like for instance a bullet hits an enemy and has to be removed before another bullet that was fired first.

Also:
Why are you using a new enterFrame for every single bullet that gets spawned? If you already have an array that contains all the bullets you can use just ONE enterFrame and iterate through your array once per frame to move (and even remove) the bullets. I avoid using too many listeners in flash because it ruins control and it gets confusing to pass variables around using them. Also there is a tiny performance overhead to having many events, but that isn't important.
Image
User avatar
Runouw
Site Admin

Error contacting Twitter
 
Posts: 342
Joined: July 27th, 2009, 5:05 am
Location: Here...

Thumbs Up given: 3 times
Thumbs Up received: 176 times

PreviousNext

Return to Programming