Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Problem with moving group, jumping and spawning


peoplesbeer
 Share

Recommended Posts

So it's my first post and I'm sorry if it's been discussed before (which I sure it has).
I've been searching through the forum and other parts of the web for days now but I can't figure out what I'm doing wrong here and since you guys seem to be "the ones to talk to", I'll give it a shot with a few questions.

My goal is to make a simple piew piew game with my "custom" sprite.

Problem 1:
I can only get ONE enemy to move to my object. I guess there's something wrong with the function that executes the enemy group but I'm not sure where and what is causing it. Help?
Problem 2: 
I want the enemies to spawn outside of the game and then move towards my object. In the function for createEnemyBat there's an enemies.create and if I got this right that's where the spawning action is happening and I need to place the X-axis more "out of bounds"?

Problem 3:

Seems like there's a problem with my obstacles (ground only to put the player a bit off the ground). It does work if I make the if statement cursor key "up" is pressed AND player is touching down, which also makes the player jump with gravity. BUT! With that I can not make the jump sprite appear since it's removed when player is NOT touching down. Makes sense but I can't figure out any workaround on it. So as for now the player has "moon gravity" on his jumping ability.  

 

Random question:
Is it possible to add different types of enemies into same "spawn action"? In my code I want to run two different types of enemies but instead of making two functions and calls for it, can I randomly get them into one function and just use like Math.random?

 

Thanks in advance!

var game = new Phaser.Game(800,400, Phaser.AUTO, '', {preload: preload, create: create, update: update});function preload() {    game.load.image('background', 'assets/desert_BGcut.png');    game.load.image('ground', 'assets/bg-ground.png');    game.load.image('bullet', 'assets/megaman/shoot/ammo1.png');    game.load.atlasJSONHash(        'boom',        'assets/boom.png',        'assets/boom.json'    )    game.load.atlasJSONHash(        'megamanRun',        'assets/megamanRun2.png',        'assets/megamanRun2.json'    )    game.load.atlasJSONHash(        'batEnemy',        'assets/batSprite.png',        'assets/batSprite.json'    )    game.load.atlasJSONHash(        'spinnerEnemy',        'assets/spinnerSprite.png',        'assets/spinnerSprite.json'    )}var player;var obstacles;var cursors;var score = 0;var scoreText;var scoreString = '';var enemy;var bullet;var bulletTime = 0;var explosions;var spinner;function create(){    game.physics.startSystem(Phaser.Physics.ARCADE);    var background = game.add.tileSprite(0,0,800,400, 'background');    // Ground position    obstacles = game.add.group();    obstacles.enableBody = true;    var ground = obstacles.create(0, game.world.height - 43, 'ground');    ground.body.immovable = true;    // Player    player = game.add.sprite(32, game.world.height - 150, 'megamanRun');    player.scale.x = 1.2;    player.scale.y = 1.2;    game.physics.arcade.enable(player);    player.body.bounce.y = 0.2;    player.body.gravity.y = 300;    player.body.collideWorldBounds = true;    // Player animations on keyboard input    player.animations.add('left', [19,13,12,10,14], 15, true);    player.animations.add('right', [15,16,17,11,18], 15, true);    player.animations.add('up', [27], 10, true);    // Bullets    bullets = game.add.group();    bullets.enableBody = true;    bullets.physicsBodyType = Phaser.Physics.ARCADE;    bullets.createMultiple(30, 'bullet');    bullets.setAll('anchor.x', -2);    bullets.setAll('anchor.y', -1.5);    bullets.setAll('outOfBoundsKill', true);    bullets.setAll('checkWorldBounds', true);    // Target hit    explosions = game.add.group();    explosions.createMultiple(30, 'boom');    explosions.forEach(setupEnemy, this);    // Score board    scoreText = game.add.text(16,16, 'Score: 0', {fontSize: '32px', fill: '#fff'});    scoreString = "Score: ";    // Keyboard input variables    cursors = game.input.keyboard.createCursorKeys();    fire = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);    // Enemies    enemies = game.add.group();    enemies.enableBody = true;    enemies.physicsBodyType = Phaser.Physics.ARCADE;    // Function creating enemies    function createEnemyBat(){        for(var y = 0; y < 5; y++){            for(var x = 0; x < 2; x++){                enemy = enemies.create(game.world.randomX, Math.random() * 500, 'batEnemy', game.rnd.integerInRange(0,game.world.width));                enemy.anchor.setTo(0.5,0.5);                enemy.animations.add('fly', [0,1,2,1,0], 20, true);                enemy.play('fly');            }        }    }    createEnemyBat();}// Function that makes boom-animation on killfunction setupEnemy(enemy){    enemy.animations.add('boom', [0,1,2,3,4,5]);}function update(){    game.physics.arcade.collide(player, obstacles);    game.physics.arcade.overlap(player, enemies, gameOver, null, this);    game.physics.arcade.overlap(bullets, enemies, collisionHandler, null, this);    game.physics.arcade.moveToObject(enemy, player, 80);    // Player keyboard shortcuts    player.body.velocity.x = 0;    if(cursors.left.isDown){        player.body.velocity.x = -150;        player.animations.play('left');    }    else if (cursors.right.isDown){        player.body.velocity.x = 150;        player.animations.play('right');    }    else {        player.animations.stop();        player.frame = 22;    }    if(cursors.up.isDown){        player.body.velocity.y = -200;        player.animations.play('up');    }    else if(fire.isDown){        fireBullet();    }}// Function for bullets, speed and starting pointfunction fireBullet(){    if(game.time.now > bulletTime){        bullet = bullets.getFirstExists(false);        if(bullet){            player.animations.add('bullet', [9], 10, true);            bullet.reset(player.x, player.y + 10);            bullet.body.velocity.x = 400;            bulletTime = game.time.now + 200;        }    }}// Kill bullet when it goes off screenfunction killBullet(bullet){    bullet.kill();}// Collision handler with explosion on bullet hit and score incrementfunction collisionHandler (bullet, enemy){    bullet.kill();    enemy.kill();    score += 1;    scoreText.text = scoreString + score;    var explosion = explosions.getFirstExists(false);    explosion.reset(enemy.body.x, enemy.body.y);    explosion.play('boom', 30, false, true);}// Collision handler with explosion on enemy vs player hitfunction gameOver (enemy, player){    enemy.kill();    player.kill();    var explosion = explosions.getFirstExists(false);    explosion.reset(enemy.body.x, enemy.body.y);    explosion.play('boom', 30, false, true);}
Link to comment
Share on other sites

Problem 1:

 

You're making a bat and storing it in the variable "enemy". If you make another one then "enemy" will point to your new one while your old one is still hanging around in the game, moving towards the old destination. In update, try a for loop over your enemies.children and calling moveToObject that way.

for (var i = 0; i < enemies.children.length; i++) {  var enemy = enemies.children[i];  game.physics.arcade.moveToObject(enemy, player, 80);}

It'll look something like that.

 

Problem 2:

 

Yes. ( =

 

game.world.randomX will pick a number between [0, width) of the world. Try picking -10 or width + 10 or something.

 

Problem 3:

 

I'm not sure what you're asking. This sentence doesn't quite make sense to me: "With that I can not make the jump sprite appear since it's removed when player is NOT touching down."

 

Random:

 

Yes. Functions are first-class constructs, which means you can pass'em around, store them in objects... that means you can store an array of functions that create enemies and pick a random one, call it, and get a new enemy randomly.

Link to comment
Share on other sites

Wow, thanks!
Problem 1: solved.
Problem 2: solved.

Problem 3: it started when I went through a tutorial for Phaser with a player standing on an image (ground) with obstacles in the map (large horizontal bars to jump onto). This tutorial stated that

if(cursors.up.isDown && player.is.touching.down){        player.body.velocity.y = -200;        player.animations.play('up');

where && player.is.touching.down was needed for the player to have some sort of "normal" gravity in his jumps. Otherwise you could tap up-key on and on and on and he would just rise higher and higher. A jump in a jump. 

As I used that "template" I noticed that if I added the "needed" part for normal jump, my players sprite for "jump-action" wouldn't work because as soon as he's leaving the ground - one of the statements is now false where as both need to be true for the if statement to fully execute. I get that, but I don't know how to work my way around it, guess it'll be placed on my "nice to have"-list. ;)

So it's really not necessary to fix at the moment.

(And hard to explain, obviously, lol)

 

Random:
Great, as I suspected. I'll get to it right away!

Link to comment
Share on other sites

Ah, I gotcha. It's these lines that are causing the problem:

else {player.animations.stop();player.frame = 22;}

You're telling Phaser to stop the animations. I find it very helpful to separate physics and animation a bit. Set a walking boolean and a jumping boolean. Then decide later what to play based on what's true right now, that kind of thing.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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