Jump to content

problem about piling up object


taowei
 Share

Recommended Posts

Hello,  I have a problem about arcard, I want to create a game to pile up blocks to make a building like a tower higher. when the second block fall onto the first one, everything is OK, but when the third one(or the fourth, fifth,etc) come, the block began to overlap and fall down. could anyone help me? thanks and very appreciate! the relative code is in 'playstate' object.

var Game = (function() {
    var game;
    var floorBlocks;
    var rope;
    var ropeTween;
    var stick;
    var background;
    var score = 0;
    var scoreText;
    var creatingItem;
    var bottomBound;
    var boundGroup;
    var moveBlock;
    var movingBlock;
    var bootState = {
        init: function () {
            console.log('boot')
        },
        create: function () {
            //适配屏幕
            game.scale.scaleMode = Phaser.ScaleManager.EXACT_FIT;
            game.scale.parentIsWindow = true;

            //  启动物理引擎
            game.physics.startSystem(Phaser.Physics.ARCADE);

            game.state.start('load');
        }
    };

    var loadState = {
        init: function () {
            console.log('load')
        },
        preload: function () {
            game.load.crossOrigin = "Anonymous";//允许图片资源跨域
            game.load.atlas('mysprite','./assets/spritesheet.png', './assets/sprites.json');
            game.load.image('rope','./assets/rope.png');
            game.load.image('stick','./assets/stick.png');
            game.load.image('floorBlock','./assets/floorBlock.png');
            game.load.image('bottomBound','./assets/bottomBound.png');
        },
        create: function () {
            game.state.start('menu');
        }
    };

    var menuState = {
        init: function () {
          console.log('menur')
        },
        create: function () {
            //首页背景添加
            game.add.sprite(0, 0,'mysprite','homebg');
            //首页标题
            this.gameTitle = game.add.sprite(320,380,'mysprite',"homeTitle");
            this.gameTitle.anchor.setTo(0.5,0.5);
            //开始游戏按钮
            this.playButton = game.add.sprite(320,750,'mysprite',"gameStart");
            this.playButton.anchor.setTo(0.5,0.5);
            this.playButton.inputEnabled = true;
            this.playButton.events.onInputDown.add(this.start, this);
            //游戏规则按钮
            this.ruleButton = game.add.sprite(320,950,'mysprite',"ruleBtn");
            this.ruleButton.anchor.setTo(0.5,0.5);
            this.ruleButton.inputEnabled = true;
            this.ruleButton.events.onInputDown.add(this.showRule, this);
            //游戏规则说明
            this.rulePop = game.add.sprite(40,150,'mysprite',"rulePop");
            this.rulePop.inputEnabled = true;
            this.rulePop.visible = false;
            this.rulePop.events.onInputDown.add(this.hideRule, this);
        },
        start: function () {
            game.state.start('play');
        },
        showRule: function () {
            this.rulePop.visible = true;
        },
        hideRule: function () {
            this.rulePop.visible = false;
        }
    };

    var playState = {
        init: function () {
            score = 0;
        },
        create: function () {
            //  背景添加
            background = game.add.tileSprite(0, 0, 640, 1136, 'mysprite','playbg');

            //杆子
            stick = game.add.sprite(350,200,'stick');


            //绳子
            rope = game.add.sprite(350,200,'rope');
            console.log(rope.x,rope.y);
            rope.angle = 45;
            console.log(rope.x,rope.y);

            ropeTween = game.add.tween(rope).to({angle:-45}, 1000, Phaser.Easing.Linear.None, true).yoyo(true).loop(true);

            //底边界
            boundGroup = game.add.group();
            boundGroup.enableBody = true;
            bottomBound = boundGroup.create(0,game.world.height-30,'bottomBound');

            //添加楼层块
            //floorBlocks = game.add.physicsGroup(Phaser.Physics.ARCADE);
            //floorBlocks = game.add.physicsGroup(Phaser.Physics.P2JS);
            floorBlocks = game.add.group();
            floorBlocks.enableBody = true;
            floorBlocks.setAll('body.collideWorldBounds', true);
            addNewBlock();
            moveBlock(floorBlocks.getChildAt(floorBlocks.length-1));
            //  The score
            scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });

/*            game.input.onTap.add(dropBlock, this);*/
            game.input.onTap.add(function () {
                stopMoveBlock();
                dropBlock();
                disactiveRope();
                addNewBlock();
                moveBlock(floorBlocks.getChildAt(floorBlocks.length-1));
            }, this);
        },
        update: function () {
            if(rope.angle > 0){
                movingBlock.y = 250 + Math.cos(rope.angle*Math.PI/180)*200;
                movingBlock.x = 180 - Math.sin(rope.angle*Math.PI/180)*200;
            }
            else{
                movingBlock.x = 180 + Math.sin(-rope.angle*Math.PI/180)*200;
                movingBlock.y = 250 + Math.cos(-rope.angle*Math.PI/180)*200;
            }
            //  碰撞检测及回调处理
/*            game.physics.arcade.overlap(floorBlocks, floorBlocks, function (a,b) {
                a.body.velocity.y = 0;
                a.body.gravity.y = 0;
                a.body.immovable = true;
            }, null, this);*/
/*            game.physics.arcade.overlap(floorBlocks, boundGroup, function (a,b) {
                console.log('asds')
                addNewBlock();
                moveBlock(floorBlocks.getChildAt(floorBlocks.length-1));
            }, null, this);*/
/*            game.physics.arcade.collide(floorBlocks);*/
/*            game.physics.arcade.overlap(floorBlocks,function () {
                console.log('12312321')
            });*/
            game.physics.arcade.collide(floorBlocks);
        }
    };
    var overState = {
        highScore:null,
        init: function () {
            this.highScore = localStorage.getItem('highScore');
            if(!this.highScore){
                localStorage.setItem('highScore',score);
                this.highScore = score;
            }
            else if(this.highScore < score){
                localStorage.setItem('highScore',score);
                this.highScore = score;
            }
        },
        create: function () {
            if(score < 50){
                game.add.sprite(0, 0, 'gameOver1');
            }
            else{
                game.add.sprite(0, 0, 'gameOver2');
            }
            //重开游戏按钮
            this.restartButton = this.game.add.sprite(200,860,"restartBtn");
            this.restartButton.anchor.setTo(0.5,0.5);
            this.restartButton.inputEnabled = true;
            this.restartButton.events.onInputDown.add(this.restart, this);
            game.add.text(410,355,this.highScore,{font:"30px Microsoft Yahei",fill:"#764DA4"})
            var scoreText = game.add.text(game.world.centerX,540,score,{font:"80px Microsoft Yahei",fill:"#764DA4"})
            scoreText.anchor.setTo(0.5,0.5);
        },

    restart: function () {
            game.state.start('play');
        },
    };
    //initiate game settings
    function init() {
        game = new Phaser.Game(640, 1080, Phaser.AUTO, '');
        game.state.add('boot', bootState);
        game.state.add('load', loadState);
        game.state.add('menu', menuState);
        game.state.add('play', playState);
        game.state.add('over', overState);
        game.state.start('boot');
    }

    function preload() {
        game.load.atlas('mysprite','./assets/spritesheet.png', './assets/sprites.json')
        game.load.crossOrigin = "Anonymous";//允许图片资源跨域
    }


    function create() {



    }

    function update() {



    }

    //移动楼层
    function moveBlock(block) {
        movingBlock = block;
    }
    //停止移动楼层
    function stopMoveBlock() {
        movingBlock = {};
    }

    //添加新楼层
    function addNewBlock() {
        var newBlock = floorBlocks.create(180,350,'floorBlock');
        newBlock.body.enable = true;
    }

    //晃动绳子
    function activeRope() {
        ropeTween.resume();
    }
    //停止晃动绳子
    function disactiveRope() {
        ropeTween.pause();
    }
    function endGame() {
        if(playerDie){
            game.state.start('over');
        }
    }
    //扔下楼层块
    function dropBlock() {
        var block = floorBlocks.getChildAt(floorBlocks.length-1)
        block.body.gravity.y = 500;
        block.body.collideWorldBounds = true;
    }
    return {
        init:init,
    };
})();


Game.init();
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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