Be a Supporter!

Projectile problem

  • 373 Views
  • 5 Replies
New Topic Respond to this Topic
gameObject
gameObject
  • Member since: Jan. 4, 2012
  • Offline.
Forum Stats
Member
Level 03
Game Developer
Projectile problem 2012-09-17 04:50:57 Reply

Good day,
I'm working on a side scrolling beat 'em up game where my player is able to perform a fireball attack.
So far in my code, the projectile instantiates onto the stage fine. I'm am having trouble getting my projectile to move.
Here's what I have:
Player class

private function chargeFireball(event:Event):void
		{
			if(punchKeyDown)
			{
				chargeTimer.start();
			}
				else if((!punchKeyDown) && (chargeTimer.running))
				{
					chargeTimer.stop();
					chargeTimer.reset();
					
				}
		}
		
		private function shootFireball(event:KeyboardEvent):void
		{
			if(!punchKeyDown)
			{
				if(chargeTimer.currentCount == 1)
				{
					fireballCharged = true;
					trace("fireball attack!");
				}
			}
		}

Fireball class

private var speed:Number = 4;
 
		public function shoot(object:MovieClip):void
		{
			if(object.scaleX < 0)
			{
				this.x -= speed;
			}
			
			else if(object.scaleX > 0)
			{
				this.x += speed;
			}
		}

Engine class

public function shootFireball(event:Event):void
		{
			fireball = new Fireball;
			if(player.fireballCharged == true)
			{
				addChild(fireball);
				fireball.x = player.x;
				fireball.y = player.y;
				player.fireballCharged = false;
				
				fireball.shoot(player);
			}
			
			if(fireball.x > stage.stageWidth && fireball.x < stage.stageWidth)
			{
				removeChild(fireball);
			}
		}

Can someone tell me what i'm missing?
Regards.

UnknownFury
UnknownFury
  • Member since: Aug. 10, 2005
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Projectile problem 2012-09-17 05:27:22 Reply

Looks like shoot on FireBall, which moves the ball, is only going to be called once.

Simplified version of your code:

if(player.fireballCharged == true)
{
	player.fireballCharged = false;	
	fireball.shoot(player);
}

Notice how once it's shot once then it's not going to be called again. Second to that, you're creating a new instance of fireball every time this function is called, so that last part is going to work how you intend it either. I'd probably do something along the lines of:

Fireball:

// constructor - set direction to shoot ball in
public function FireBall (dir : Number = 0) : void
{
	this.direction = dir / Math.abs(dir);
}

public function move () : void
{
	this.x += speed * dir;
}

Engine:

public function shootFireball () : void
{
	// if they're not charged then do nothing
	if(!player.fireballCharged)
		return;

	// create a new fireball
	var fireball : FireBall = new FireBall(player.scaleX);
	addChild(fireball);
	fireball.x = player.x;
	fireball.y = player.y;
	// put it in an array
	fireballs.push(fireball);

	player.fireballCharged = false;
}

// This is the main update loop
public function update (e : Event) : void
{
	// move all the fireballs and check they're in bounds
	for(var i : int = fireballs.length - 1; i >= 0; i--)
	{
		var fireball : FireBall = fireballs[i];
		fireball.move();
		
		if(fireball.x > stage.stageWidth && fireball.x < stage.stageWidth)
		{
			// remove from stage then from array
			removeChild(fireball);
			fireballs.splice(i, 1);
		}
	}

}

Note that this code might not work as is, as A. I just woke up and B. I wrote this straight into NG so I haven't checked it in flash, but the logic should be sound assuming I understand what you want correctly.So basically what I've done is when you shoot a fireball set its direction and put it in an array, then in the main update loop move all fireballs and if they're out of bounds remove them.

A few side notes:
Putting trace statements in your code can be useful for debugging. If something is not happening when you think it should then put in a trace where the function is called to see if it's actually being called. If not, then use traces to find why that function's not called etc.

A very minor thing,

boolean == true

Is pointless. You're comparing true/false to true/false and getting back a true/false value. Instead of == true you can do:

if (boolean) { ... }

And instead of comparing to false:

if (!boolean) { ... }

Add it's good practice to only have ENTER_FRAME event. It looks like you've got a few. I would just have one in the 'engine' which calls any needed functions.

gameObject
gameObject
  • Member since: Jan. 4, 2012
  • Offline.
Forum Stats
Member
Level 03
Game Developer
Response to Projectile problem 2012-09-17 05:57:30 Reply

Thank you very kindly man. I'll give it a go after dinner.

Thaaanks!

gameObject
gameObject
  • Member since: Jan. 4, 2012
  • Offline.
Forum Stats
Member
Level 03
Game Developer
Response to Projectile problem 2012-09-18 09:24:43 Reply

Are there any other ways I can go about this in terms of which way my player is facing? Was I on the right track? I did what you said

Fireball

public function Fireball(dir:Number): void 
		{
			this.direction = dir / Math.abs(dir);
		}
		
		public function shoot():void
		{
			this.x += speed * dir;
		}

Engine

public function shootFireball(event:Event):void
		{
			fireball = new Fireball(player.scaleX);
			if(player.fireballCharged)
			{
				addChild(fireball);
				fireball.x = player.x;
				fireball.y = player.y;
                                player.fireballCharged = false;
				
			}
			
			if(fireball.x > stage.stageWidth && fireball.x < stage.stageWidth)
			{
				removeChild(fireball);
			}
		}

But get these errors:

1119: Access of possibly undefined property direction through a reference with static type scripts:Fireball.
1120: Access of undefined property direction.

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to Projectile problem 2012-09-18 12:53:51 Reply

You haven't defined "direction" variable anywhere.

var direction:Number = 0;

And btw, you posted this same problem over a week ago, and I told you to put your moving code in an enterframe but you haven't done anything; your code now is exactly same as before. Are you even trying to solve your own problem? To me it looks like you are looking for a copy/paste answer. Just saying that it doesn't look like you are learning anything here.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

gameObject
gameObject
  • Member since: Jan. 4, 2012
  • Offline.
Forum Stats
Member
Level 03
Game Developer
Response to Projectile problem 2012-09-19 02:02:54 Reply

And btw, you posted this same problem over a week ago, and I told you to put your moving code in an enterframe but you haven't done anything; your code now is exactly same as before. Are you even trying to solve your own problem? To me it looks like you are looking for a copy/paste answer. Just saying that it doesn't look like you are learning anything here.

I'm having a little difficulty understanding the whole event thing. Thanks.