Jump to content

Help with function


mrxj88
 Share

Recommended Posts

var fish, background, speed = 5, food, score = 0, scoreText; 
var mainState = {
    preload: function (){ 


        game.load.spritesheet('fish', 'assets/spritesheet.png', 100, 100, 24);
        game.load.image('food', 'assets/FishFood.png');
        game.load.image('BG', 'assets/scroller.png');
        game.load.image('can', 'assets/can.png');
        game.load.audio('beep', 'assets/beep.wav');
    },

    create: function(){ 
        
        game.physics.startSystem(Phaser.Physics.ARCADE);
        game.physics.setBoundsToWorld();
        game.stage.backgroundColor = '71c5cf'
        background = game.add.tileSprite(0, 0, 800, 600, 'BG');
        game.world.setBounds(0, 0, 800, 600)
        
        food = game.add.sprite( 600, 0, 'food');
        game.physics.arcade.enable(food);
        food.enableBody = true;
        food.scale.setTo(0.4,0.4)
        food.body.gravity.y = 6;
        
        fish = game.add.sprite(0, 200, 'fish');
        game.physics.arcade.enable(fish);
        fish.animations.add('right', [0, 1, 2, 3, 4, 5], 6, true );
        fish.animations.add('left', [ 7, 8, 9, 10, 11], 6, true);
    
        scoreText = game.add.text(16, 16, 'Score: 0', { fontSize: '20px', fill: '#000' } );
        beep = game.add.audio('beep');
        
        cans = game.add.group();
        cans.enableBody = true;
        for (var y = 0; y < 4; y++) {
            for (var x = 0; x < 10; x++)
            {
                var can = cans.create(200 + x * 15, y * 10, 'can');
                can.name = 'can' + x.toString() + y.toString();
                can.checkWorldBounds = true;
                can.events.onOutOfBounds.add(canOut, this);
                can.body.velocity.y = 50 + Math.random() * 200;
            }
        }

    },
    
    

    update: function() {
        
        food.x -= 2;
        cans.x -=1;
        
        background.tilePosition.x -= 2;
        
        if (game.physics.arcade.overlap(fish, cans)){
            fish.kill();
        }
        
        if (game.physics.arcade.overlap(fish, food)){
            food.kill();
            beep.play();
            score += 10;
            scoreText.text = 'Score: ' + score;
        };

        if(game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
            fish.x += speed;
            fish.animations.play('right', false); 
        }
        else if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)){
            fish.x -= speed;
            fish.animations.play('left', false);
        }
        else if (game.input.keyboard.isDown(Phaser.Keyboard.UP)){
            fish.y -= speed;
        }
        else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)){
            fish.y += speed;
        }

    },
  
        canOut: function(can){
            
        can.reset(can.x, 0);
        can.body.velocity.y = 50 + Math.random() * 200;
}


};


var game = new Phaser.Game(400, 600, Phaser.CANVAS, 'gameArea' {canOut:,canOutS});
game.state.add('main', mainState); 
game.state.start('main');

 

Link to comment
Share on other sites

Sorry i posted the code but my question was gone lol. but anyway im new to phaser and javascript and was wondering why it wont let me create a function called canOut. it says its not defined... sorry if my code looks like crap still learning.

Link to comment
Share on other sites

ha ha, no worries, your code looks fine!

`var foo = 'foo'` in JS appends a variable named foo to the global, usually window, i.e. `var foo` is equivalent to `window.foo`, as in, window is an object that is exposed to the JS context and variables, by default, get appended to it, however, they can also be appended to your own object, which is what you have done with the following:

var mainState = {
  canOut: ...
}

`canOut` now exists as `window.mainState.canOut`, although in JS you don't need to explicitly call `window`, doing `console.log(mainState.canOut)` will work.

However, I mention this window stuff (which is known as global pollution) because later on you try to reference the function but `window.canOut` does not exist, try passing in `mainState.canOut` or moving `canOut` out of the `mainState` object.

i.e.

function canOut () {
  ...
}

var game = new Phaser.Game(400, 600, Phaser.CANVAS, 'gameArea', {
  canOut: canOut
})

Note I added a comma and removed one from your Phaser.Game invocation. Also, I'm not sure you can use the constructor like this (I'm no Phaser expert), but canOut is now defined (note that using the same name might be slightly confusing, its certainly not wrong but you have a global canOut function and then an object with a property called canOut, which equals the globally defined canOut function).

Note also that appending lots of stuff to window is a code smell, but getting away from it requires a bit of extra effort, which you don't need to do at the moment as it adds complexity, learn that stuff a little later.

I hope that makes some sense.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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