HTML5GAME :: HTML5GAME

달력

82025  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

가끔은 1 시간 만에 게임을 쓰기 위해 도전하고 싶습니다.

게임 내용은 먼저 컴퓨터가 4개의 다른 색상의 판넬에서 터치를 하게됩니다. 그리고 플레이어는 컴퓨터가 진행한 터치를 그대로 따로 하면 됩니다.



소스 코드는 다음과 같습니다.

var StateMain = {

    preload: function() {

        game.load.spritesheet("squares","images/squares.png",250,250);

        game.load.audio("note1","audio/1.mp3");

        game.load.audio("note2","audio/2.mp3");

        game.load.audio("note3","audio/3.mp3");

        game.load.audio("note4","audio/4.mp3");

        game.load.audio("bang","audio/bang.mp3");

    },

    create: function() {

        //define an array to hold the random song

        this.songArray = [];

        //define the notes and sound effects

        var note1 = game.add.audio("note1");

        var note2 = game.add.audio("note2");

        var note3 = game.add.audio("note3");

        var note4 = game.add.audio("note4");

        this.bang = game.add.audio("bang");

        //play the notes on an array for easy indexing

        this.noteArray = [note1, note2, note3, note4];

        //lock the click for while the song is playing

        this.clickLock = true;

        //make the blocks

        var red = this.getBlock(0);

        var blue = this.getBlock(1);

        var yellow = this.getBlock(2);

        var green = this.getBlock(3);

        //

        //put the blocks in a group

        //this will make it easy to center all the blocks

        //

        this.blocks = game.add.group();

        this.blocks.add(red);

        this.blocks.add(blue);

        this.blocks.add(yellow);

        this.blocks.add(green);

        //

        //position the blocks

        //all the postions were set at 0

        //so we just need to set some of the positions

        //

        blue.x = 250;

        yellow.y = 250;

        green.y = 250;

        green.x = 250;

        //make the blocks 80% of the width of the screen

        this.blocks.width = game.width * .8;

        //scale the height to match the width

        this.blocks.scale.y = this.blocks.scale.x;

        //center the blocks on the screen

        this.blocks.x = game.width / 2 - this.blocks.width / 2;

        this.blocks.y = game.height / 2 - this.blocks.height / 2;

        //alpha the blocks

        this.resetBlocks();

        //add a note to the song

        this.addToSong();

        //play the song

        this.playSong();

    },

    /**

     * [getBlock make a sprite, and add the click listener]

     * @param  {[type]} frame [this is the color of the block]

     * @return {[group]}  

     */

    getBlock: function(frame) {

        var block = game.add.sprite(0, 0, "squares");

        block.frame = frame;

        block.inputEnabled = true;

        block.events.onInputDown.add(this.clickBlock, this);

        return block;

    },

    /**

     * [resetBlocks turn all the blocks to 20% alpha]

     * @return {[none]}

     */

    resetBlocks: function() {

        this.blocks.forEach(function(block) {

            block.alpha = .2;

        });

    },

    /**

     * [addToSong push a random note on the songArray]

     */

    addToSong: function() {

        var s = game.rnd.integerInRange(0, 3);

        this.songArray.push(s);

    },

    playSong: function() {

        //set the playIndex to negative one

        this.playIndex = -1;

        //start the timer to play the notes

        this.songTimer = game.time.events.loop(Phaser.Timer.SECOND, this.nextNote, this);

    },

    nextNote: function() {

        //alpha the blocks

        this.resetBlocks();

        //advance the playIndex

        //we start at -1 so the first time the playIndex will be 0

        this.playIndex++;

        //if the play index is equal to the songArray length then

        //song is over

        //

        if (this.playIndex == this.songArray.length) {

            this.clickLock = false;

            this.clickIndex = 0;

            game.time.events.remove(this.songTimer);

            return;

        }

        //get the number of the note from the song

        var note = this.songArray[this.playIndex];

        //play the note

        this.playNote(note);

    },

    playNote: function(note) {

        //get the sound object from the note array

        var sound = this.noteArray[note];

        sound.play();

        //get the block that goes with the sound

        var block = this.blocks.getChildAt(note);

        //light it up!

        block.alpha = 1;

        //turn off the lights after a 1/4 of a second delay

        game.time.events.add(Phaser.Timer.SECOND / 4, this.resetBlocks, this);

    },

    clickBlock: function(target) {

        //if the song is playing return

        if (this.clickLock == true) {

            return;

        }

        //get the frame from the square clicked

        var index = target.frame;

        //if the index is equal to the number

        //in the songArray then the user

        //has pressed the right notes

        if (index == this.songArray[this.clickIndex]) {

            //get the note from the songArray

            var note = this.songArray[this.clickIndex];

            //play the note

            this.playNote(note);

            this.clickIndex++;

            //if the clickIndex is equal to the songArray length we have

            //reached the end of the song

            if (this.clickIndex == this.songArray.length) {

                this.clickLock = true;

                this.playIndex = -1;

                this.addToSong();

                this.playSong();

                return;

            }

        } else {

            //wrong note!

            //Game Over

            this.bang.play();

            this.clickLock = true;

            game.state.start("StateOver");

        }

    },

    update: function() {}

}



Posted by 마스터킹
|


Toast메시지는 페이드 인 및 페이드 아웃되는 문자 메시지입니다. 유리를 올린 사람과 축배를하고 유리를 다시 내리는 것과 비슷하기 때문에 토스트 메시지라는 이름이 붙어 있습니다.


여기에 예제가 있습니다.

var StateMain = {

    preload: function() {

        game.load.image("toastBack", "images/back.png");

        game.load.image("btnToast", "images/btnGenerate.png");

    },

    create: function() {

 

        //SET UP TEST BUTTON

     this.btnToast=game.add.sprite(game.world.centerX,game.height*.25,"btnToast");

     this.btnToast.anchor.set(0.5,0.5);

     this.btnToast.inputEnabled=true;

     this.btnToast.events.onInputDown.add(this.testToast,this);

    },

    testToast: function() {

        this.btnToast.visible=false;

        //get an instance of a toast object

        //and place it in postion

        var toast = this.makeToast("TEST!");

        toast.x = game.world.centerX;

        toast.y = game.world.height * .8;

        //fade in the toast object

        this.fadeIn(toast);

    },

    makeToast: function(message) {

        var toastGroup = game.add.group();

        var toastText = game.add.text(0, 0, message);

        var toastBack = game.add.sprite(0, 0, "toastBack");

        toastBack.width = game.width * .9;

        //

        //

        //SET ANCHORS

        toastText.anchor.set(0.5, 0.5);

        toastBack.anchor.set(0.5, 0.5);

        //

        //ADD THE TEXT AND SPRITE GRAPHIC TO THE TOAST GROUP

        toastGroup.add(toastBack);

        toastGroup.add(toastText);

        toastGroup.alpha = 0;

        //

        return toastGroup;

    },

    fadeIn: function(obj) {

        var tween = game.add.tween(obj).to({

            alpha: 1

        }, 1000, Phaser.Easing.Linear.None, true);

        tween.onComplete.add(this.delay, this);

    },

    delay:function(obj)

    {

        //WHEN TWEEN IS DONE PAUSE HERE FOR DELAY

        //SET A FADE OBJECT IN THE SCOPE OF THE STATE,

        //SINCE WE CAN NOT PASS THE OBJECT IN THE TIMER

     this.fadeObj=obj;

     game.time.events.add(Phaser.Timer.SECOND*2, this.delayDone, this);

    },

    delayDone:function()

    {

        //NOW THAT DELAY IS DONE CALL FADE OUT

     this.fadeOut(this.fadeObj);

    },

    fadeOut: function(obj) {

        var tween = game.add.tween(obj).to({

            alpha: 0

        }, 1000, Phaser.Easing.Linear.None, true);

        tween.onComplete.add(this.fadeDone, this);

    },

    fadeDone:function()

    {

        this.btnToast.visible=true;

    },

    update: function() {}

}



Posted by 마스터킹
|

최근에 나에게 매우 유용했던 것은 두 물체 사이의 각도를 구하거나 마우스와 중심 문자 사이의 각도를 얻을 수있게하는 것입니다. 예를 들어, 클릭하고 발사하려는 우주선이있는 게임에서 우주선을 돌릴 수있는 각도를 알아야합니다. 각도를 얻는 실제 수학은 약간 복잡하고 나는 그것을 이해하는 척하지는 않지만, 내가 가지고있는 매우 유용한 코드가 있습니다.

getAngle: function(obj1, obj2) {

// angle in radians

var angleRadians = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);

// angle in degrees

var angleDeg = (Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x) * 180 / Math.PI);

return angleDeg;

},


각도를 얻으려면이 두 객체를 전달해야합니다.

var angle = this.getAngle (goodGuy, monster);


다음은 우주선을 돌리기 위해 마우스와 우주선 사이의 각도를 사용하는 예입니다.

우주선이 마우스를 가리 키도록하려면 아무 곳이나 클릭하십시오.


그리고 예제 코드는 다음과 같습니다.

var StateMain = {

    preload: function() {

        game.load.image("ship", "images/ship.png");

    },

    create: function() {

        this.ship = game.add.sprite(game.world.centerX, game.world.centerY, "ship");

        this.ship.anchor.set(0.5, 0.5);

        game.input.onUp.add(this.clickCanvas, this);

    },

    clickCanvas: function() {

        //make an object from the mouse postion

        var obj1 = {

            x: game.input.x,

            y: game.input.y

        };

        //get the angle between the mouse and the ship and assign that to the

        //ship's angle

        this.ship.angle = this.getAngle(obj1, this.ship);

    },

    getAngle: function(obj1, obj2) {

        //I use the offset because the ship is pointing down

        //at the 6 o'clock position

        //set to 0 if your sprite is facing 3 o'clock

        //set to 180 if your sprite is facing 9 o'clock

        //set to 270 if your sprite is facing 12 o'clock

        //

        offSet = 90;

        // angle in radians

        var angleRadians = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);

        // angle in degrees

        var angleDeg = (Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x) * 180 / Math.PI);

        //add the offset

        angleDeg += offSet;

        return angleDeg;

    },

    update: function() {}

}





Posted by 마스터킹
|