更换精灵时出现奇怪的错误

时间:2018-04-10 19:41:20

标签: javascript animation sprite phaser-framework

var game = new Phaser.Game(400, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });

//creating score value and onscreen text
var score = 0;
var scoreText;

function preload() {
    // preload assets
    game.load.image('sky', 'assets/img/sky.png');
    game.load.image('ground', 'assets/img/platform.png');
    game.load.image('star','assets/img/star.png');
    game.load.spritesheet('baddie', 'assets/img/baddie.png', 32, 48);
}

function create() {
    // place your assets
    //enabling Arcade Physics system
    game.physics.startSystem(Phaser.Physics.ARCADE);

    //adding a background
    game.add.sprite(0, 0, 'sky');

    //a group containing the ground and platforms to jump on
    platforms = game.add.group();

    //enabling physics for any object in this group
    platforms.enableBody = true;

    //creating the ground
    var ground = platforms.create(0, game.world.height - 64, 'ground');

    //scaling to fit the width of the game
    ground.scale.setTo(2, 2);

    //stops ground from falling once player jumps on it
    ground.body.immovable = true;

    //create five ledges
    var ledge = platforms.create(-300, 400, 'ground');
    ledge.body.immovable = true;
    ledge = platforms.create(200, 400, 'ground');
    ledge.body.immovable = true;
    ledge = platforms.create(100, 300, 'ground');
    ledge.body.immovable = true;
    ledge = platforms.create(-200, 200, 'ground');
    ledge.body.immovable = true;
    ledge = platforms.create(300, 100, 'ground');
    ledge.body.immovable = true;

    //create the player and its settings
    player = game.add.sprite(32, game.world.height - 150, 'baddie');

    //enabling physics on player
    game.physics.arcade.enable(player);

    //giving player a slight bounce
    player.body.bounce.y = 0.2;
    player.body.gravity.y = 300;
    player.body.collideWorldBounds = true;

    //walking left and right animations
    player.animations.add('left', [0, 1], 10, true);
    player.animations.add('right', [2, 3], 10, true);

    //create group for stars
    stars = game.add.group();
    stars.enableBody = true;

    //creating 12 stars evenly spaced apart
    for (var i = 0; i < 12; i++) {
        //create a star inside of the 'stars' group each 33 px apart
        var star = stars.create(i * 33, 0, 'star');

        //giving it gravity
        star.body.gravity.y = 6;

        //giving each star a random bounce value
        star.body.bounce.y = 0.7 + Math.random() * 0.2;
    }
    scoreText = game.add.text(16, 16, 'Score: 0', {fontSize: '32px', fill: '#000'});
}

function update() {
    // run game loop
    //collide player and stars with platforms
    var hitPlatform = game.physics.arcade.collide(player, platforms);

    //built in keyboard manager
    cursors = game.input.keyboard.createCursorKeys();

    //reset players velocity (movement)
    player.body.velocity.x = 0;

    //moving with arrow keys
    if (cursors.left.isDown) {
        //move to left
        player.body.velocity.x = -150;
        player.animations.play('left');
    }
    else if (cursors.right.isDown) {
        //move right
        player.body.velocity.x = 150;
        player.animations.play('right');
    }
    else {
        //stand still
        player.animations.stop();
        player.frame = 2;
    }

    //allow player to jump if touching ground
    if (cursors.up.isDown && player.body.touching.down && hitPlatform) {
        player.body.velocity.y = -350;
    }

    //checking for collision with stars and platforms
    game.physics.arcade.collide(stars, platforms);

    //checking if player overlaps with star
    game.physics.arcade.overlap(player, stars, collectStar, null, this);
}

function collectStar (player,star) {

    //removes star from screen
    star.kill();

    //add and update score
    score += 10;
    scoreText.text = 'Score: ' + score;
}

我似乎无法弄清楚为什么,但是一旦我更改了精灵(动画帧比原始动画帧少),就会弹出一些奇怪的错误。这两条错误消息说:

未捕获的TypeError:无法读取null的“getFrameIndexes”属性

未捕获的ReferenceError:未定义星星

动画帧范围似乎没有超出范围,我已经在create函数中定义了我的'stars'变量。关于为什么我会收到这些奇怪的错误消息的任何想法? (播放器的spirtesheet附在下面)

spritesheet

1 个答案:

答案 0 :(得分:0)

我无法评论,所以我回答,

错误显示未捕获的ReferenceError:未定义星星

我认为你必须将明星声明为全局变量,因为你在create()和update()中使用它,但是在collectStar()

中使用它的本地定义是完整的