简体   繁体   中英

Trying to add bulelts to javascript game ( to the spacebar)

I am almost done my game, I just need to add bullets that fire when you press space

I do have some code so far that I used from a tutorial, however it points towards the mouse. I know that I have to move it into the key-handler, but I don't know how.

I also don't know how to get rid of the wade part, I know it comes from a .json file but I want not to

Heres the code:

                    var nextFireTime = lastFireTime + 1 / fireRate;
                    var time = wade.getAppTime();
                    if (wade.isMouseDown() && time >= nextFireTime)
                    {
                        lastFireTime = time;
                        // create bullet...
                    }


                    wade.setMainLoopCallback(function()
                    {
                        // code to execute several times per second
                    }, 'fire');

                      if (wade.isMouseDown())
                        {
                            var spacemanPosition = spacemanImage.getPosition();
                            var spacemanSize = spacemanImage.getSize();
                            var sprite = new Sprite('images/alien.png');
                            var bullet = new SceneObject(sprite, 0, shipPosition.x, shipPosition.y - shipSize.y / 2);
                            wade.addSceneObject(bullet);
                            bullet.moveTo(shipPosition.x, -500, 600);
                        }

                     bullet.onMoveComplete = function()
                    {
                    wade.removeSceneObject(this);
                    };

                     var lastFireTime = 0;
                    var fireRate = 5;

To use the spacebar instead of a mousepress you have to change

wade.isMouseDown()

to

wade.isKeyDown('space')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM