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

Bullets in a space game

New Here ,
Dec 02, 2018 Dec 02, 2018

Am trying to figure out the best way to do bullets and be able to update a score and actually get the x and y coordinate of the enemy ship I kill.  Currently I have a fire_bullet function that references a bullet projectile symbol and I do the hit object test there.  If I do the hit test in the main program loop it only registers some of the bullets but if I do the test in the projectile symbol I can send back certain info to the main program.  Like the x and y coordinate of the enemy ship.  Here is my current code.

function fireBullet(event:KeyboardEvent):void

{     

    if (event.keyCode == 13)

    {

    var angle:Number = ship1.rotation;

    angle = angle*Math.PI/180;

    clip = new Projectile();

    addChild(clip)

    laz.play()

    clip.x = ship1.x+nose*Math.cos(angle);

    clip.y = ship1.y+nose*Math.sin(angle);

    //trace(angle);

    clip.xmov = speed*Math.cos(angle);

    clip.ymov = speed*Math.sin(angle);

    clipList[clickCount] = clip;

    clickCount++

    shotCount ++;

    clip.explo = explo

    //clip.wavecount

    clip.alien = alienList[waveCount]//List[waveCount]

    }

}

I have a clipList[clickCount] array i was going to try to use in the main program to test for hits but could not get that working.

Here's the code for the projectile symbol.

addEventListener(Event.ENTER_FRAME, moveBullet)

var i:int = 0

function moveBullet(event:Event):void

    {

        this.x += this.xmov;

        this.y += this.ymov;

        if (this.alien != null)

        if (this.hitTestObject(this.alien))

        {

            //trace("Hit")

            this.alien.gotoAndPlay(6)

            this.explo.play()

            i++

            //removeChild(this)

            if (i % 4 == 0)

            {

                //alienList[waveCount].rotation+=180

                this.alien.rotation = 0

                this.alien.x = 10000000

                this.alien.y = 200

            }

        }

       

    }

The current way I deal with scoring is by sending the alien to  this.alien.x = 10000000  and then thes code that sets next_wave to false which launches the wave function in the main code and setting jack to true increments the score.

if (alienList[waveCount].x >= 5000)

                {

                        //removeChild(alien)

                        jack = true

                        nextWave = false

                        waveCount++

                }

function wave():void

    kill = false

    attack = 40

    l++   

    if (l % 6 == 0)

    {       

        if (jack==true)

        {

            removeChild(alien)

            jack = false

            score += 10

        }

    }

       

    i++   

    if (i % 48 == 0)

    {

        addChild(alien)

        n = Math.random() * (foodList.length - 1) //random food for enemy to go after

        //trace ("n",n, "foodList.length",foodList.length)

        alienList[waveCount1] = alien;

        attack = Math.random()* 40

        //trace("attack",attack)

        if( attack <= 5)//remember it is 10

        {

            kill = true

        }

       

        m = Math.random()* 20

        if( m < 10)

        {

            //trace("mupper",m)

            alienList[waveCount1].rotation = 0//reset to down

            alienList[waveCount1].y = 25

            alienList[waveCount1].x = Math.random() * (stage.stageWidth - alienList[waveCount].width)

        }

        else

        {

            //trace("mlower",m)

            alienList[waveCount1].rotation = 0 //reset to down

            alienList[waveCount1].rotation +=180  // flip to up

            alienList[waveCount1].y = 310

            alienList[waveCount1].x = Math.random() * (stage.stageWidth - alienList[waveCount].width)   

        }

        waveCount1++

        nextWave = true

    }   

}

So this works but I want to change it because I am having some timing issues.  I currently have the alienList[waveCount] (alien symbol) calling a gotoAndPlay(6) that has the explosion in it.  I want to separate the explosion from the alien symbol and be able to take the enemy x and y coordinate and kill the enemyShip right away and have an explosion symbol be right at the x and y coordinate  of the  removeChild(alien)  with something like this:

alienList[waveCount].x = tempx

alienList[waveCount].y= tempy

explosion.x = tempx

explosion.y = tempy

removeChild(alien)

I wish you could have global variables in the symbols.  Or that _root. worked.  I read about that but I guess that was a previous version of actionscript.  Any help in this matter will be appreciated.  Thanks.

137
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
no replies

Have something to add?

Join the conversation