Jump to content

Help with sprites


edvinsdainis
 Share

Recommended Posts

Hi guys, for it's deadline to finish this game, and I am stuck with this last bug, the idea is that there is cat catching eggs, and at this point if egg falls to him, it just stands at he's head, and is not being destroyed.

Here is the code.

import Phaser from 'phaser'

//Define all variables to use this(game need's to use global object for chaining).
let cat = this.cat;
let egg = this.egg;
let eggs = this.eggs;
let firingTimer = this.firingTimer;
let score = this.score;
let scoreString = this.scoreString;
let scoreTxt = this.scoreTxt
let leftMove = this.leftMove
let rightMove = this.rightMove

export default class extends Phaser.State {

    preload () {
        // game.load.atlas('cat-run-move', './assets/animations/cat-run-stand.png', './assets/animations/cat-run-stay.json');
    }

    create() {

        this.game.scale.fullScreenScaleMode = Phaser.ScaleManager.SHOW_ALL;
        this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        this.game.scale.refresh();

        this.physics.startSystem(Phaser.Physics.ARCADE);

        // this.background = this.add.image(0, 0, "main-bg-deskt");
        // this.background.height = this.game.height;
        // this.background.width = this.game.width;

        this.leftMove = false;
        this.rightMove = false;
        ///123

        if (window.innerWidth < 640) {

            this.background = this.add.image(0, 0, "main-bg-mob");
            this.background.height = this.game.height;
            this.background.width = this.game.width;

            let soundBtn = game.add.sprite(game.world.centerX+250, game.world.centerY-485, 'sound-on')
            soundBtn.anchor.setTo(0.5, 0.5);
            soundBtn.scale.x = 0.8
            soundBtn.scale.y = 0.8

            //Make cat and anchor it to middle of screen
            this.cat = game.add.sprite(game.world.centerX, game.world.centerY+500, 'cat-moving')
            this.cat.anchor.setTo(0.5, 0.5);
            this.cat.scale.x = 0.5
            this.cat.scale.y = 0.5
            this.physics.enable(this.cat, Phaser.Physics.ARCADE)

            this.cat.animations.add('walk', [4,5,9,14], 18, true)

            // let leftBtn = game.add.button(50, 50, 'ok-soc', null, this)
            // leftBtn.anchor.setTo(0.5, 0.5)
            //
            // leftBtn.events.onInputDown.add(function () {
            //     this.leftMove = true;
            // })
            //
            // let rightBtn = game.add.button(200, 200, 'ok-soc', null, this)
            // rightBtn.anchor.setTo(0.5, 0.5)
            //
            // rightBtn.events.onInputDown.add(function () {
            //     this.rightMove = true;
            // })

        }

        if (window.innerWidth > 641) {

            let soundBtn = game.add.sprite(game.world.centerX+550, game.world.centerY-970, 'sound-on')
            soundBtn.scale.x = 1.5
            soundBtn.scale.y = 1.5
            soundBtn.anchor.setTo(0.5, 0.5);

            //Make cat and anchor it to middle of screen
            this.cat = game.add.sprite(game.world.centerX, game.world.centerY+500, 'cat-moving')
            this.cat.anchor.setTo(0.5, 0.5);
            this.cat.scale.x = 0.5
            this.cat.scale.y = 0.5
            this.physics.enable(this.cat, Phaser.Physics.ARCADE)

            this.cat.animations.add('walk', [4,9,14,20], 12, true)

        }

        if (window.innerWidth >= 1024) {

            let soundBtn = game.add.sprite(game.world.centerX+600, game.world.centerY-470, 'sound-on')
            soundBtn.anchor.setTo(0.5, 0.5);

            //Make cat and anchor it to middle of screen
            this.cat = game.add.sprite(game.world.centerX, game.world.centerY+500, 'cat-moving')
            this.cat.anchor.setTo(0.5, 0.5);
            this.physics.enable(this.cat, Phaser.Physics.ARCADE)

            this.cat.animations.add('walk', [4,9,14,20], 12, true)

        }

        //Enable physics for cat, set it moving limits inside screen(if physics not enabled for model, you will get body null eror)
        this.physics.enable(this.cat, Phaser.Physics.ARCADE)
        this.cat.enableBody = true;
        this.cat.body.collideWorldBounds = true;

        //Egg firing timer
        this.firingTimer = 0;

        this.eggs = game.add.group();
        this.eggs.enableBody = true;
        this.eggs.createMultiple(100, 'egg')
        this.eggs.setAll('outOfBoundsKill', true);
        this.eggs.setAll('checkWorldBounds', true);

        //Add score counter
        this.score = 0;
        this.scoreString = 'Eggs cached: '
        this.scoreTxt = game.add.text(10, 10, this.scoreString + this.score, { font: '34px Arial', fill: '#fff' });

    }

    //This method is like watcher for all events in game

    update () {

        //Swipe controls for tablets/phones

        let swipeCoordX;
        let    swipeCoordY;
        let    swipeCoordX2;
        let    swipeCoordY2;
        let   swipeMinDistance = 10;

        game.input.onDown.add(function(pointer) {

            swipeCoordX = pointer.clientX;
            swipeCoordY = pointer.clientY;

        }, this);

        game.input.onUp.add(function(pointer) {

            swipeCoordX2 = pointer.clientX;
            swipeCoordY2 = pointer.clientY;

            if(swipeCoordX2 < swipeCoordX - swipeMinDistance){

                console.log("left");
                this.cat.x -= 5;
                this.cat.scale.x = 0.5
                // this.cat.body.velocity.x = 300
                this.cat.animations.play('walk')

            }else if(swipeCoordX2 > swipeCoordX + swipeMinDistance){

                console.log("right");
                this.cat.x += 5;
                this.cat.scale.x = -0.5
                // this.cat.body.velocity.x = -300
                this.cat.animations.play('walk')

            }else if(swipeCoordY2 < swipeCoordY - swipeMinDistance){

                console.log("up");

            }else if(swipeCoordY2 > swipeCoordY + swipeMinDistance){

                console.log("down");

            }

        }, this);

        //Add controls for cat

        // if (this.leftMove) {
        //     this.cat.x -= 20;
        //     this.cat.scale.x = 0.5
        //     this.cat.animations.play('walk')
        // }
        //
        // if (this.rightMove) {
        //     this.cat.x += 20;
        //     this.cat.scale.x = -0.5
        //     this.cat.animations.play('walk')
        // }

        this.cat.body.velocity.x=0

        if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
        {
            this.cat.x -= 10;
            this.cat.scale.x = 0.5
            this.cat.animations.play('walk')

        }
        else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
        {
            this.cat.x += 10;
            this.cat.scale.x = -0.5
            this.cat.animations.play('walk')
        } else {
            this.cat.loadTexture('cat-moving', 8)
        }

        if (this.game.time.now > this.firingTimer) {
            this.generateEgg()
        }

        this.physics.arcade.collide(this.eggs, this.cat, this.aquireEgg, null, this)

        // setTimeout(function () {
        //     this.state.start('Result')
        // }, 20000)

    }

    generateEgg () {

        this.egg = this.eggs.getFirstExists(false);

        if (this.egg) {
            this.egg.reset(this.game.world.randomX, 40);
            this.firingTimer = this.game.time.now + 500;
            this.egg.body.gravity.y = 1200;
            this.eggs.add(this.egg)
        }

        // this.egg.reset(this.game.world.randomX, 40);
        // this.firingTimer = this.game.time.now + 350;
        // this.egg.body.gravity.y = 1200;
        // this.eggs.add(this.egg)
    }

    aquireEgg (egg) {
        // this.egg.destroy();
        // this.egg.body = null;

        this.eggs.sort('exists')
        console.log(this.egg)
        this.egg.destroy(this.eggs.getIndex(this.eggs.getFirstExists(false)))

        this.score += 1;
        this.scoreTxt.text = this.scoreString + this.score
    }

}
Link to comment
Share on other sites

Hi @edvinsdainis ,

Try this on your aquireEgg function:

aquireEgg(egg) {
        
        eggs.kill(egg);

        this.score += 1;
        this.scoreTxt.text = this.scoreString + this.score;
    }

"egg" is passed as a parameter. Surely, in this case "this.egg" does not reference the received parameter. You have to use "egg" without the prefix "this".

And in your generateEgg():

generateEgg () {

        this.egg = this.eggs.getFirstExists(false);

        if (this.egg) {
            this.egg.reset(this.game.world.randomX, 40);
            this.firingTimer = this.game.time.now + 500;
            this.egg.body.gravity.y = 1200;
            this.egg.revive();
        }
    }

I think that with groups it is better to use the kill () and revive () methods. This way you don't have to create new objects and you take away work from the GC.

On the other hand, you should remove the "game.input.onUp.add" and "game.input.onDown.add" methods from the update function and put them in the create function. Now, you are adding 30/60 listeners every second.

Greetings.

Link to comment
Share on other sites

LOL, this fixed the whole thing :D

aquireEgg (cat, egg) {
    //cat is unnused parameter, but it needs to be here, so egg would be reference to this.egg, othervise it will point to this.cat
    egg.destroy();
    this.score += 1;
    this.scoreTxt.text = this.scoreString + this.score
}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...