Skip to main content
Known Participant
March 25, 2011
Question

How to setup bullet factory/bullet switch for the same player object?

  • March 25, 2011
  • 1 reply
  • 829 views

Hi guys,

I'm currently making my own shoot'em up game where the player can switch  between two player types which will fire their own bullet type in this  case, squares and circles. I've setup the player switch function via  pressing "space" button (switching between the two player types) and the  default bullet type is "squares" and I'm not sure to approach the  coding with changing the bullet type to "circles".

I've written 2 classes for the bullet and player setup:

Player Class: Creates instance of bullet on screen and enables the switch player type function



Bullet Class: Enables bullet properies and bullet types


Player Class:

Code:
function onKeyDown(event:KeyboardEvent):void
        {

if (event.keyCode == 32) //// pressing/holding "space bar" to function
            {
                {
                 this.gotoAndStop(3); ///changing to other player type via frame 3
                  }
                }
            
private function onKeyUp(event:KeyboardEvent):void //// letting go of "space bar" returns to default player type - frame 1
        {

    if (event.keyCode == 32)
            {
            this.gotoAndStop(1);
            }

private function shootBullet():void
        {

         //Create bullet instance and add it to the stage
            var bullet:Bullet = new Bullet(bullet_Vx, bullet_Vy, bullet_StartX, bullet_StartY, "square");
            parent.addChild(bullet);
            }
Bullet Class

Code:
public function Bullet(vx:Number, vy:Number, startX:Number, startY:Number, bulletType:String)
        {
            //Assign velocity and start position using values
            //supplied to the parameters
            this._vx = vx;
            this._vy = vy;
            this.x = startX;
            this.y = startY;
          
            //Find bullet type
            switch (bulletType) /// Declaring the bullet types
            {
                case "square" :
                    gotoAndStop(SQUARE);
                    break;


                case "circle" :
                    gotoAndStop(CIRCLE);
                    break;
            }
So in a nutshell, I'm wondering how you would suggest to code  bullet switch with player switch, any suggestions or help would be  greatly appreciated.

Thanks

Jonesy
This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
March 25, 2011

does each player have his own class?  if yes, each should instantiate the appropriate bullet type.  if no, your controller class that instantiates players and changes players should change bullet types.

Known Participant
March 25, 2011

Hi kglad,

To answer your question, yes to the latter, I've setup the game so there is only one player character that can switch between two different player types with only one player class document. I was wondering how would you code to switch bullets along with the switch player type? Here is my attempt below trying to implement this functionality.

Player class

 function onKeyDown(event:KeyboardEvent):void          
          {
if (event.keyCode == 32) //// pressing/holding "space bar" to function
            {
                {
                 this.gotoAndStop(3); ///changing to other player type via frame 3
                 case "circle" :
                  gotoAndStop(CIRCLE);

                 }
private function onKeyUp(event:KeyboardEvent):void
          {           
             if (event.keyCode == 32)
          {
          this.gotoAndStop(1);
          case "square" :
                gotoAndStop(SQUARE);

               }

My logic was to reference to bullet class using case "Bullet type" but instead I got error 1084: Syntax error: expecting identifer before case for both lines with "Case" involved. I alternatively tried the line "switch (bulletType):" instead of Case, but  I get a similar error 1084: Syntax erroe: expecting leftbrace before colon. What I'm trying to say is I'm tried a few things, not sure where I should headed in regards of coding such functionality.

Thanks

Jonesy

kglad
Community Expert
Community Expert
March 25, 2011

there's no such code.  you can use switch-case code but your code makes  no sense as it stands.  if you had some variable (eg, bulletVar) that took the value of  "square" or the value of "circle",  you could use



private function onKeyUp(event:KeyboardEvent):void
          {           
             if (event.keyCode == 32)
          {
       
switch(bulletVar){
          case "square" :
                gotoAndStop(SQUARE);
  //<--needs to be a string or int
break;

case "circle":
gotoAndStop(CIRCLE);  // <-- string or int
break;

}

}
               }