metalslug87

world bounds problem on moveToXY method

4 posts in this topic

Hello :),

I have problem with world bounds, when the player goes right he reaches the invisible barrier and he can't go through. He can't even shoot behind this barrier.

I am attaching the code with test game, please help me, I've read the phaser docs and I can't find any usefull info about my problem.

Game controls:
Z - shoot
Left Mouse Click - walk

phaser-example.zip

Share this post


Link to post
Share on other sites

What problem are you trying to solve? There isn't really a question in your post.

Share this post


Link to post
Share on other sites

I just want to move the player on the whole map not just in the game box (400px x 600px), the whole map is about 12000px x 5000px, these lines of code could be helpfull to describe my problem:
 

var GameState = function(game) {
};

GameState.prototype.preload = function() {
    this.game.load.tilemap('map-1', 'assets/map-1.json', null, Phaser.Tilemap.TILED_JSON);
    this.game.load.image('tiles-1', 'assets/tileset-1.png');
    this.game.load.spritesheet('player', 'assets/player.png', 10, 14);
    this.game.load.image('playerBullet', 'assets/playerBullet.png');
};

GameState.prototype.create = function() {
    this.map;
    this.tileset;
    this.layer;
    this.player;
    this.bg;
    this.rapidKeyPressBlock = false;
    this.playerX;
    this.playerY;

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

    this.map = this.game.add.tilemap('map-1');
    this.map.addTilesetImage('tileset-1', 'tiles-1');
    this.layerDecorations = this.map.createLayer('decorations');
    this.layer = this.map.createLayer('map');
    

    this.player = this.game.add.sprite(200, 250, 'player');
    this.game.physics.enable(this.player, Phaser.Physics.ARCADE);
    this.player.body.setSize(10, 14, 0, 0);
    this.game.camera.follow(this.player);

    this.playerBullets = this.game.add.group();
    this.playerBullets.enableBody = true;  
    this.playerBullets.physicsBodyType = Phaser.Physics.ARCADE;
    this.playerBullets.setAll('checkWorldBounds', true);
    this.playerBullets.setAll('outOfBoundsKill', true);

    this.layer.debug = true;

    this.shootButton = this.game.input.keyboard.addKey(Phaser.Keyboard.Z);

    this.game.world.setBounds(0, 0, 12000, 5000);
    this.layer.resizeWorld();
    //this.game.world.resize(12000, 12000);

};

GameState.prototype.update = function() {

    if (this.shootButton.isDown && this.rapidKeyPressBlock == false) {      
        this.playerShootButton();
        this.rapidKeyPressBlock = true;
        this.game.time.events.add(Phaser.Timer.SECOND * 0.2, function() {
            this.rapidKeyPressBlock = false;
        }, this);
    };

    //player movement
    this.player.body.velocity.x = 0;
    this.player.body.velocity.y = 0;

    if (this.game.input.activePointer.isDown) {
        this.playerX = this.input.activePointer.x;
        this.playerY = this.input.activePointer.y; 
        console.log("X: " + this.playerX + " Y: " + this.playerY);
    }

    if (this.game.physics.arcade.distanceToXY(this.player, this.playerX, this.playerY) > 5) {
        this.game.physics.arcade.moveToXY(this.player, this.playerX, this.playerY, 100, 0);
    }
};
// end UPDATE

GameState.prototype.playerShootButton = function() {
        this.playerBullet = this.playerBullets.create(this.player.position.x + 8, this.player.position.y + 5, 'playerBullet');  
        this.playerBulletX = this.input.activePointer.x + this.game.rnd.integerInRange(-15, 15);
        this.playerBulletY = this.input.activePointer.y + this.game.rnd.integerInRange(-15, 15);
        game.physics.arcade.moveToXY(this.playerBullet, this.playerBulletX, this.playerBulletY, 350, 0);
        this.playerBullet.body.outOfBoundsKill = true;
};

GameState.prototype.render = function() {
    // this.game.debug.bodyInfo(this.player, 16, 24);
};

var game = new Phaser.Game(400, 600, Phaser.AUTO, 'game');
game.state.add('game', GameState, true);

 

Share this post


Link to post
Share on other sites

Solved, I've replaced:
 

this.playerX = this.input.activePointer.x;
this.playerY = this.input.activePointer.y;

to:

this.playerX = this.input.activePointer.worldX;
this.playerY = this.input.activePointer.worldY;

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.