Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Mar 25, 2011 Mar 25, 2011

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
TOPICS
ActionScript
861
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2011 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 25, 2011 Mar 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2011 Mar 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;

}

}
               }


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 28, 2011 Mar 28, 2011

Hi kglad again,


Thanks for your input and help, I've used your suggestion and i've got it working now but coded it slightly differently so I thought out of interest, you just going to share the results how I did it and also for anyone else who may be interested in implementing such functionality. Anyway here goes ....

In the Player doc, I created a new variable called char-type using that to divide the two different bullet types -

--------------------------------------------------------------------

public class Player extends MovieClip
    {

        private var _bounce:Number;
        private var _angle:Number;
        private var _shotFired:Boolean;
       
        public var char_type:int;


public function Player()
        {

         char_type = 1;

          {

function onKeyDown(event:KeyboardEvent):void
        {

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

private function onKeyUp(event:KeyboardEvent):void
     
  {

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

private function shootBullet():void
        {
             {   
            var bullet_type:String = "square";
           
            if (char_type == 3)
            {
                bullet_type = "circle";
            }
            if (char_type == 1)
            {
                bullet_type = "square";
            }

           var bullet:Bullet = new Bullet(bullet_Vx, bullet_Vy, bullet_StartX, bullet_StartY, bullet_type);
            parent.addChild(bullet);
                }

--------------------------------------------------------

Anyway thanks kglad again and I hope that may of been useful for someone else reading

Jonesy

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2011 Mar 28, 2011
LATEST

you're welcome.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines