Search the Community

Showing results for tags 'collision'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • HTML5 Game Coding
    • News
    • Game Showcase
    • Coding and Game Design
  • Frameworks
    • Phaser
    • Pixi.js
    • Babylon.js
    • Panda.js
    • melonJS
    • Haxe JS
    • Kiwi.js
  • General
    • General Talk
  • Business
    • Collaborations (un-paid)
    • Jobs (Hiring and Freelance)
    • Services Offered

Found 430 results

  1. Hi there, I have been trying to setup a collision system in my rpg game but I have encountered this error. I'm trying to create a collision between the player and the collisionLayer. When I start the state I receive this error: map1State.js:55 Uncaught TypeError: Cannot read property 'collide' of undefined at map1State.update (map1State.js:55) at Phaser.StateManager.update (phaser.js:28633) at Phaser.Game.updateLogic (phaser.js:34992) at Phaser.Game.update (phaser.js:34935) at Phaser.RequestAnimationFrame.updateRAF (phaser.js:62702) at _onLoop (phaser.js:62685) Could someone please explain to me why this happens? Heres my code for the state var map1State = function(game) {}; map1State.prototype = { preload: function () { game.load.tilemap('map1', 'assets/mapcollide.json', null, Phaser.Tilemap.TILED_JSON); game.load.image('map1_set', 'assets/tilemap.png'); game.load.image('player', 'assets/player.png'); }, create: function () { map1 = game.add.tilemap('map1', 32, 32); map1.addTilesetImage('set_00','map1_set'); tilemapLayer = map1.createLayer('Tilemap'); collisionLayer = map1.createLayer('Collision'); game.world.setBounds(0, 0, 1280, 960); game.physics.startSystem(Phaser.Physics.P2JS); player = game.add.sprite(game.world.centerX, game.world.centerY, 'player'); game.physics.p2.enable(player); player.body.fixedRotation = true; cursors = game.input.keyboard.createCursorKeys(); game.camera.follow(player, Phaser.Camera.FOLLOW_LOCKON, 0.1, 0.1); }, update: function () { player.body.setZeroVelocity(); if (cursors.up.isDown) { player.body.moveUp(300) } else if (cursors.down.isDown) { player.body.moveDown(300); } if (cursors.left.isDown) { player.body.velocity.x = -300; } else if (cursors.right.isDown) { player.body.moveRight(300); } game.physics.P2JS.collide(player, collisionLayer); } };
  2. Hi, I loaded my tiles with game.physics.p2.convertTilemap() and it works. However, each tile is a square block and my "player" is floating on slanted tiles (similer to problem mentioned in here). Using Tiled editor's "Tile Collision Editor", i defined the polygon I want but Phaser does not seem to pick it up. Part of the exported JSON looks like this (tells me that the polyline was created): "tilesets":[ { "firstgid":1, "image":"tiles.png", "imageheight":374, "imagewidth":374, "margin":0, "name":"tiles", "properties": { }, "spacing":2, "tileheight":32, "tiles": { "57": { "objectgroup": { "draworder":"index", "height":0, "name":"", "objects":[ { "height":0, "name":"", "polyline":[ { "x":0, "y":0 }, { "x":32, "y":-32 }], "properties": { }, "rotation":0, "type":"", "visible":true, "width":0, "x":0, "y":32 }], "opacity":1, "type":"objectgroup", "visible":true, "width":0, "x":0, "y":0 } } }, "tilewidth":32 }, { "firstgid":122, "image":"items.png", "imageheight":270, "imagewidth":270, "margin":0, "name":"items", "properties": { }, "spacing":2, "tileheight":32, "tilewidth":32 }, { "firstgid":186, "image":"sprites.png", "imageheight":100, "imagewidth":238, "margin":0, "name":"sprites", "properties": { }, "spacing":2, "tileheight":32, "tilewidth":32 }]How do I load the tile's collision polyline/polygon in Phaser when loading the tiled layer?
  3. I am creating a platforming game that has multiple different platforms. Some platforms slope up and others slope down. Upward Sloping Platforms * If the character moves right, his y position will increase * If the character moves left, his y position will decrease * The angle property of these platforms is negative Downward Sloping Platforms * If the character moves right, his y position will decrease * If the character moves left, his y position will increase; * The angle property of these platforms is positive The following tries to find the colliding platform. My update function looks something like this game.physics.arcade.collide(hero, platforms, function() { currentPlatform = platforms.children.filter(p => p.getBounds().contains(hero.x, hero.y))[0]; gameState.moveRight(); gameState.moveLeft(); }); } I need this platform to calculate whether the character will move upwards or downwards. Here is an example if (cursors.right.isDown) { if (currentPlatform) { hero.y = currentPlatform.angle < 0 ? hero.y - 0.04 : hero.y + 0.04; } } This obviously doesn't work, and the 0.04 value is hard coded, because I can't figure out how to increment with an angle. I've also tried calling sprite.body.touching, which returns the following Object {none: true, up: false, down: false, left: false, right: false} I am completely lost right now.
  4. http://playground.babylonjs.com/index.html#WP0JL8 I can rotate camera, but cannot move. `camera.cameraDirection` is changing, but camera position is not.
  5. Hi, I was wondering how can I do with P2JS to not collide with a given group ( I don't want the player to collide with others players ).
  6. Hi, I'm learning phaser and got this issue. The collision between the player and the map is not working since I moved the player to its own file (player.js) as an object Im calling this in the update function on the play.js game.physics.arcade.collide(this.player.sprite, this.layer); I'm creating the tile map in the play.js createWorld: function () { this.map = game.add.tilemap('map'); this.map.addTilesetImage('tileset'); this.layer = this.map.createLayer('Tile Layer 1'); this.layer.resizeWorld(); this.map.setCollision(1); }, Here's the player.js file function player() { this.preload = function () { }; this.create = function () { this.sprite = game.add.sprite(game.width / 2, game.height / 2, 'atlas', 'player01'); this.sprite.anchor.setTo(0.5, 0.5); game.physics.arcade.enable(this.sprite); this.sprite.body.gravity.y = 500; this.cursor = game.input.keyboard.createCursorKeys(); }; this.update = function () { }; } Originally the collision worked well before putting the player in a different file, but I can't make the collision work now.
  7. Hi everyone, I am making a pinball-like game and there can be more than one ball at once. Is it possible to ignore collision between balls and leave it for some other objects? Many thanks.
  8. Hello! I am working with Phaser and having some problem to collide sprite with tiles, the sprite can walk through everything. on create: map = game.add.tilemap('ninomap'); map.addTilesetImage('tilesheet', 'tilesheet'); ground = map.createLayer('background'); ground.resizeWorld(); wcollide = map.createLayer('walls'); map.setCollision(1624, true, wcollide); Where 1624 is the index of tile to collide. I've been looking the posts on this forums, but couldn't get it to work yet, so i'm asking for help to understand the collide system on Phaser and how can i get my sprite to collide with certain tile indexes or layers.
  9. Hi, in my render function, I have the following: render: function() { this.game.debug.body(this.player); this.game.debug.body(this.redMonstarsGroup); }, Now, the player, has a little green box around him, however the monster (or the group of monsters) has nothing and I get a warning: " gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source);" HOW TO I GET A LITTLE GREEN DEBUG BODY ON A GROUP OF OBJECTS?
  10. I made a pretty good Frogger clone using PhaserJS, but when refactoring the code, I ran into a very, very strange problem. My vehicles currently have a velocity of 200. When Frogger collides with one of them, that vehicle's velocity drops in half. Frogger also gains the velocity of that object. In update this.game.physics.arcade.collide(frogger, vehicles, this.loseLife); Here's my vehicles code objects.forEach(o => this.game.physics.arcade.enable(o)); [normalCar1, normalCar2, normalCar3, specialVehicle1, specialVehicle2, specialVehicle3, emergencyVehicle1, emergencyVehicle2, emergencyVehicle3].forEach(function(object) { object.body.velocity.x = object.direction == 'right' ? 200 : -200; }); Here is my loseLife() function loseLife: function() { /*switch (frogger.health) { case 5: lifeFive.destroy(); break; case 4: lifeFour.destroy(); break; case 3: lifeThree.destroy(); break; case 2: lifeTwo.destroy(); break; case 1: lifeOne.destroy(); break; }*/ frogger.damage(1); gameState.resetPosition(); gameState.resetRows(); /*if (!frogger.alive) { clearInterval(sinkLillies); game.state.start('gameover'); }*/ }, The important method here is resetPosition() which moves Frogger back to the starting position resetPosition: function() { frogger.alpha = 0; frogger.x = this.game.world.centerX; frogger.y = this.game.world.height - 18; game.time.events.add(1000, function() { this.game.timer = 50000; frogger.alpha = 1; }, this); }, The important thing here is that I do not specify that I want the velocity to increase Hopefully, a video will make it easier to understand https://webmshare.com/DmoAQ Edit: A temporary working solution I was able to "solve" this by adding this line in the resetPosition() function frogger.body.velocity.x = 0; And to remove that weird behavior of the car slowing down objects.forEach(function(object) { this.game.physics.arcade.enable(object); object.body.immovable = true; }); This is a hack though. I shouldn't have to explicitly add these lines, because collisions shouldn't cause this behavior in the first place.
  11. I was working with a popular online tutorial in making an infinite runner game. All goes well, except there seems to be this one instance that pushed the player off the screen. (keeping player at velocity 0 instead of -platformSpeed). I think the issue is with this line this.player.body.velocity.x = this.player.body.touching.down ? -this.levelSpeed: 0; It seems to happen when the player catches the very corner of the platform, and isdown does not get set, then the player is "pushed" onto the platform. But this is just a logical guess as it happens too fast to debug. The collision check happens in a line above this.platformPool.forEachAlive(function (platform, index) { this.game.physics.arcade.collide(this.player, platform); if (platform.length && platform.children[platform.length - 1].right < 0) { platform.kill(); } }, this); My full code is here, but I can't figure a way to get the assets to load http://jsbin.com/tufisagoho/edit?html,js,output (updated link with Hosted Assets)
  12. Ok I am wanting to put a material on the ground that when my car hits it it's slowed a little and when the zombie hit is they are slowed alot (and such a material exists non newtonian fluids). What do I need to tell my object so I can see if they are touching without having them bounce off everything.
  13. Hi @all again, is it possible to get the first point of contact if two objects with physics collide? Of course i can get the positions of each object by itself, but not the point where their bodies contact at the first time, or? If yes, how?
  14. I am really struggling with collision on CSV tilemaps. I am creating the CSV string in the create function before doing the Phaser tilemap stuff. The map is rendering as expected along with the player sprite but collisions are not happening. The CSV map consists currently of 1s and 0s but I'll add more later. 0 should be impassable. Here is what I've got in the create function. this.game.cache.addTilemap('map', null, csvContent, Phaser.Tilemap.CSV); this.map = this.game.add.tilemap('map', 16, 16); this.map.addTilesetImage('dungeontile'); this.map.setCollision(0); //I don't think this is doing what I think it does. this.layer = this.map.createLayer(0); this.layer.resizeWorld(); In the update function, I have this: this.game.physics.arcade.collide(this.player, this.layer); I think that is the important code. I also have arcade physics enabled for the game and player. Sorry if I'm missing something. Any thoughts on why this isn't working?
  15. I'm making a simple bug-catching game where the main character catches the bugs using the kill() function when they overlap. It's working, except that he can bounce on the bugs. How can I stop him from being able to bounce on them? The update function looks like this: game.physics.arcade.overlap(this.player, this.fireflies, this.takeFirefly, null, this); and the takeFirefly function looks like this: takeFirefly: function(player, firefly) { firefly.kill(); score++; this.txtScore.setText( score.toString() ); }, main.js
  16. I m building a little platformer-type game and am having this problem: When the player is moving really fast, (basically when he falls a large distance) he no longer collides with stuff, and falls right through the floor. This problem comes up when I switch from making regular sprites in a group for floors and platforms, to loading in a tilemap from Tiled. I also notice a change, which is my only hint that something is working differently with the physics - the sprite's body.touching property no longer reads, and instead body.blocked now reads. So for example, if I let the player jump only if player.touching.down is true, I have to switch this condition when using the Tiled tilemap to player.blocked.down, because touching becomes always false. I don't really know phaser well enough to know why this is, but I just know its weird. here is how my code was before, when just making a group of sprites for platforms: this.platforms = this.add.group(); this.platforms.enableBody = true; var element; platformData.forEach(function( platform ){ element = this.platforms.create(platform.x, platform.y, 'green'); element.scale.setTo(platform.scaleX, platform.scaleY); }, this); this.platforms.setAll('body.immovable', true); // so they don't fall this.platforms.setAll('body.allowGravity', false); // so they can't be moved by other bodies and here is how it is when I load the map from Tiled: //create a tilemap object this.map = this.add.tilemap('level1'); //join the tile images to the json data this.map.addTilesetImage('tilesheet', 'gameTiles'); //create tile layers this.tileLayer = this.map.createLayer('tileLayer'); //collision layer should be collisionLayer this.map.setCollisionBetween(1, 1, true, 'tileLayer'); //resize the world to fit the layer this.tileLayer.resizeWorld(); And in update(): this.game.physics.arcade.collide(this.player, this.tileLayer); So it's weird, it collides just fine in every case, except when loading stuff from Tiled and moving really fast, and I don't really know why in one case bodies touch and in the other case bodies block, or what the difference really is supposed to mean.
  17. phaser

    I have a quick question that I need to ask. I am using Phaser 2.6.2 and was wondering if I could get some help on detecting nearby overlaps using P2 exclusively (at the moment and I would hope to keep it that way for the purpose to not manage multiple physics engines). So my question is, how do I go about this? I am new to Phaser and had troubles finding working examples. OR If anybody has good solutions to the problem of real time combat in a top down 2D game with melee weapons and how to detect hits efficiently, that would be great! I also need this type of proximity detection for my AI as well, so that would be awesome too! THANKS!
  18. When i have a scene with a bunch of static scenery props like : I have a bunch of separate LOW POLY INVISIBLE collision meshes, some grouped and some for each piece ... These low poly invisible meshes are use for Camera Collision (mesh.checkCollision = true) and used as the 'Collision Mesh' in my BJS Toolkit for things like Physics Collision and Mesh Intertection... Is Working Great. Now when it comes to optimizing the scene .... i have a 'Static Batching' feature that will combine all the meshes on the 'Babyon Static' batching layer by material. Now at right before i cache each mesh for static combining i generate is collision mesh based of the 'Collider Component'... Ok... Say i end up with 250 separate low poly INVISIBLE pieces (so draw calls should not be AS MUCH of an issue) but the 'CHECK COLLISION' computation that has to go on for each mesh ... m.checkCollision = true. Would it be BETTER for the 'Check Collision Computation Performance" to keep EACH mesh collider separate OR combine all LOW - POLY mesh colliders into one big mesh... I don't know if the invisible collision mesh implementation would be the only case where it is better to have separate meshes (even though on same default material and even though they are actually invisible) than combine meshes... Purely based on if the the mesh.checkCollision would perform BETTER checking things on 250 separate low poly pieces OR using the same vertex count of all those 250 pieces combined into a single mesh... set with checkCollision = true; WHAT SHOULD I DO
  19. Just having a play around with sps, wanted to see if I could come up with a simple collision system from scratch. Only designed to work with spheres. http://www.babylonjs-playground.com/#1C2HFL http://www.babylonjs-playground.com/#1C2HFL#1 less segments http://www.babylonjs-playground.com/#1C2HFL#2 Have a play around with number of particles, size of particles and max speed. Max speed should be less than radius, too big and the particles escape.
  20. Hi @MackeyK24 Hey I notice when I export the terrain I get to walk with the universal camera but for spheres and boxes is going through the ground , how can I add a mes-collider for the ground so the spheres roll downhill like in your last example ? var terrain = this.scene.getMeshByName('Terrain'); terrain.checkCollisions = true; But is not working things are going trough the ground, please tell me if know how to get it to work?
  21. Hello all, I am currently trying to make a simple platformer, but am having trouble detecting collision with a tile layer. After looking through many of the other forum posts here about this problem, none of the fixes seem to work for me. 'World 1' layer is the background and 'Collision' is the layer that contains the platforms. To my understanding, I should just be able to use map.setCollisionBetween() on the tiles that I want to use as the ground, which I think I did correctly. I also made sure to enable physics for the player and the collision layer. For some reason, though when I console.log(game.physics.arcade.collide(player, col)), it always returns false. When I run console.log(player.body.blocked), it only returns true when the player is on the window boundary (because of collideWorldBounds=true). Can anyone see what I'm doing wrong? var game = new Phaser.Game(800, 480, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update }); function preload() { game.load.tilemap('bgmap', 'assets/tilemaps/maps/superMarioBG.json', null, Phaser.Tilemap.TILED_JSON); game.load.image('bgtiles', 'assets/tilemaps/tiles/super_mario.png'); game.load.image('player', 'assets/sprites/georgeRight.png'); } var map; var bg; var col; var player; var cursors; var jumpButton; function create() { game.physics.startSystem(Phaser.Physics.ARCADE); game.stage.backgroundColor = '#787878'; map = game.add.tilemap('bgmap'); map.addTilesetImage('SuperMarioBros', 'bgtiles'); bg = map.createLayer('World1'); bg.scale.set(2); bg.resizeWorld(); col = map.createLayer('Collision'); col.scale.set(2); game.physics.arcade.enable(col); map.setCollisionBetween(1,40,true,col); col.resizeWorld(); player = game.add.sprite(0,game.world.height - 150,'player'); player.scale.set(1.5) ; player.anchor.setTo(.5,.5); cursors = game.input.keyboard.createCursorKeys(); jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); game.physics.arcade.enable(player); player.body.bounce.y = 0.2; player.body.gravity.y = 500; player.body.collideWorldBounds = true; } // function update() { game.physics.arcade.collide(player, col); player.body.velocity.x=0; game.camera.follow(player); if (cursors.left.isDown) { player.body.velocity.x -= 500; } else if (cursors.right.isDown) { player.body.velocity.x += 500; } if (jumpButton.isDown && player.body.onFloor()) { player.body.velocity.y = -550; } console.log(player.body.blocked); } function render() { } };
  22. layer

    Very sorry if someone has asked this before, but I searched the internet for two days now and I have yet to find the answer to this. So, what I want to know is how can I tell the size (width, height) of the platform that a player/enemy is standing on, with platforms that are based on a tilemap/layer? Take for example this game phase example starstruck: https://phaser.io/examples/v2/games/starstruck How can I tell the size of that platform the mascot falls on first? Why do I want to know this? I want to know so that I can place enemies on the map, have them go back and worth without falling off a platform. I have seen this be done when platforms are created as a group, but not with platforms in a layer. Thank you very much for any help.
  23. Hello guys I'm making a platformer game, I struggled with making collision between the player and objects created in tiled but after that I got it working with a function I created, however, when I make the rectangle rotated in tiled, in the game it comes out away from its position, take a look: this is on tiled in the game it comes out like this here is my code, createCollisionObjects is where the magic happens var game = new Phaser.Game(70 * 10, 70 * 7, Phaser.AUTO, 'game', { preload: preload, create: create, update: update, render: render }); function preload() { // tilemap game.load.tilemap( 'map', 'assets/tilemaps/maps/lvl-1.json', null, Phaser.Tilemap.TILED_JSON ); // tilemap images game.load.image('new', 'assets/tilemaps/tiles/new.png'); game.load.image('sheet', 'assets/tilemaps/tiles/sheet.png'); game.load.image('s9af', 'assets/tilemaps/tiles/s9af.png'); // player spritesheet game.load.atlasJSONHash( 'player', 'assets/spritesheets/player_walk.png', 'assets/spritesheets/player_walk.json' ); } /* * GLOBAL VARIABLES */ var map; var player; var playerSpeed = 400; var jumpTimer = 0; function create() { game.physics.startSystem(Phaser.Physics.P2JS); game.physics.p2.setBoundsToWorld(true, true, true, true, false); game.physics.p2.gravity.y = 300; game.physics.p2.restitution = 0; game.stage.backgroundColor = '#ffffff'; map = createMap().map; var start = map.objects.objective[0]; player = createPlayer({x: start.x, y: start.y}); cursors = game.input.keyboard.createCursorKeys(); } function update() { player.body.setZeroVelocity(); if (cursors.up.isDown && game.time.now > jumpTimer && checkIfCanJump()) { player.body.velocity.y = 600; jumpTimer = game.time.now + 750; } if(cursors.left.isDown) { player.body.velocity.x = -playerSpeed; player.animations.play('walk'); } else if (cursors.right.isDown) { player.body.velocity.x = playerSpeed; player.animations.play('walk'); } else { player.body.velocity.x = 0; player.animations.stop(); player.frame = 0; } game.camera.follow(player); } function render() { } function createMap() { var m, g, f, b, c; m = game.add.tilemap('map'); var mWidth = m.widthInPixels; var mHeight = m.heightInPixels; game.world.setBounds(0, 0, mWidth, mHeight); m.addTilesetImage('sheet', 'sheet'); m.addTilesetImage('new', 'new'); m.addTilesetImage('s9af', 's9af'); g = m.createLayer('ground'); f = m.createLayer('fringe'); b = m.createLayer('background'); g.resizeWorld(); f.resizeWorld(); b.resizeWorld(); createCollisionObject(m.objects.objects); return { map: m, ground: g, fringe: f }; } function createPlayer(pos) { var p = game.add.sprite(pos.x, pos.y, 'player', 'Symbol 2 instance 10000'); //p.scale.setTo(0.4, 0.4); game.physics.p2.enable(p); p.body.setZeroDamping(); p.body.fixedRotation = true; p.body.x+=p.width/2; p.body.y+=p.height/2; // animation p.animations.add( 'walk', Phaser.Animation.generateFrameNames('Symbol 2 instance 1000', 0, 7, '', 1), 10, true, false); //p.animations.play('walk-left'); return p; } function createCollisionObject(objects) { if (objects) { for(var i=0; i < objects.length; i++) { var p2 = game.physics.p2; var obj = objects[i]; var sprite = game.add.sprite(obj.x, obj.y, null); sprite.width = obj.width; sprite.height = obj.height; sprite.angle = obj.rotation; game.physics.p2.enable(sprite, false); sprite.body.x = sprite.x + sprite.width / 2; sprite.body.y = sprite.y + sprite.height / 2; sprite.body.rotation = obj.rotation; sprite.body.static = true; sprite.body.debug = true; } } } function checkIfCanJump() { var yAxis = p2.vec2.fromValues(0, 1); var result = false; for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) { var c = game.physics.p2.world.narrowphase.contactEquations[i]; if (c.bodyA === player.body.data || c.bodyB === player.body.data) { var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis if (c.bodyA === player.body.data) d *= -1; if (d > 0.5) result = true; } } return result; }
  24. I'm having an issue which I'm unsure what's happening and can't find anyone else having a similar issue. Basically, I create a group, create them, add a tween to animate them across the page. I create a player sprite and whenever the player collides with the other sprites in the group kill them and render another one. however, this only works on the first item I create. var group = game.add.group(); group.enableBody = true; group.physicsBodyType = Phaser.Physics.ARCADE; So create my group and then do a `for loop` to create all my enemies for (var i = 0; i < 2; i++) { c = group.create(game.width, 20, 'seacreatures', 'octopus0000'); c.name = "enemy" + i; c.body.immovable = true; c.alive = true; c.alignIn(laneArr[Math.floor(Math.random() * laneArr.length)], Phaser.RIGHT_CENTER); game.add.tween(c).to({ x: -200 }, speedArr[Math.floor(Math.random() * speedArr.length)], Phaser.Quadratic, true, 0, 0, false); } group.callAll('animations.add', 'animations', 'swim', Phaser.Animation.generateFrameNames('purpleFish', 0, 32, '', 4), 30, true, false); group.callAll('play', null, 'swim'); game.physics.enable(c, Phaser.Physics.ARCADE); Below this I also render the player currPlayer = game.add.sprite(50, game.height / 4 * 2 + 45, "playerFish"); game.physics.enable(currPlayer, Phaser.Physics.ARCADE); currPlayer.body.immovable = true; currPlayer.physicsBodyType = Phaser.Physics.ARCADE; currPlayer.alignIn(laneRect_three, Phaser.LEFT_CENTER); currPlayer.scale.set(1.5); So in short, the enemy travels across the `x` axis and once it collides with the player it triggers a function which looks like this, my update function: game.physics.arcade.overlap(playerFish, enemyFish, collisionHandler, null, this); then the function it calls, `collisionHandler` looks like function collisionHandler () { killedEnemies += 1; c.destroy(); c.alive = false; text.setText("You have killed " + killedEnemies + " enemies !"); console.log("<<<<<<<<"); } But like I said, it only kills the first item it hits, am I missing anything?
  25. Here is my code: var Main = function(game){ }; var velocityOfTileMoving = 10, speedOfTileGenerating = 15000, speedOfPlayerMovingRightLeft = 150, facing = 'left', sizeOfPlayer = 0.5, me, spaceBar, jumpTimer = 0; Main.prototype = { create: function() { me = this; spaceBar = me.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); //The spacing for the initial platforms me.spacing = 200; //Set the initial score me.score = 0; //Get the dimensions of the tile we are using me.tileWidth = me.game.cache.getImage('tile').width; me.tileHeight = me.game.cache.getImage('tile').height; //Set the background colour to blue me.game.stage.backgroundColor = '479cde'; me.game.add.tileSprite(0, 0, me.game.width, me.game.height, 'back'); //Enable the Arcade physics system me.game.physics.startSystem(Phaser.Physics.ARCADE); //Add a platforms group to hold all of our tiles, and create a bunch of them me.platforms = me.game.add.group(); me.platforms.enableBody = true; me.platforms.createMultiple(100, 'tile'); //Create the inital on screen platforms me.initPlatforms(); //Add the player to the screen me.createPlayer(); //Create the score label me.createScore(); //Add a platform every speedOfTileGenerating seconds me.timer = game.time.events.loop(speedOfTileGenerating, me.addPlatform, me); //Enable cursor keys so we can create some controls me.cursors = me.game.input.keyboard.createCursorKeys(); }, update: function() { //Make the sprite collide with the ground layer me.game.physics.arcade.collide(me.player, me.platforms); //region delteMaybe //Make the sprite jump when the up key is pushed // if(me.cursors.up.isDown && me.player.body.wasTouching.down) { // me.player.body.velocity.y = -1400; // } // //Make the player go left // if(me.cursors.left.isDown){ // me.player.body.velocity.x = -150; // } // //Make the player go right // if(me.cursors.right.isDown){ // me.player.body.velocity.x = 150; // } //endregion //Check if the player is touching the bottom if(me.player.body.position.y >= me.game.world.height - me.player.body.height){ me.gameOver(); } me.game.input.keyboard.onDownCallback = somethingWasPressed; me.player.body.velocity.x = 0; if(me.cursors.left.isDown) { animateRunLeft(); } else if(me.cursors.right.isDown) { animateRunRight(); } else { if(facing != 'idle') { me.player.animations.play('idle'); if(facing == 'left') { me.player.frame = 5; } else { me.player.frame = 5; } facing = 'idle'; } } if(jumpHasToOccur()) { me.player.body.velocity.y = -250; me.player.animations.play('jump'); me.game.time.events.add(Phaser.Timer.SECOND * 1.450, function(){me.player.animations.play('idle');}, this); jumpTimer = me.game.time.now + 750; } }, gameOver: function(){ this.game.state.start('Main'); }, addTile: function(x, y) { //Get a tile that is not currently on screen var tile = me.platforms.getFirstDead(); //Reset it to the specified coordinates tile.reset(x, y); tile.body.velocity.y = velocityOfTileMoving; tile.body.immovable = true; //When the tile leaves the screen, kill it tile.checkWorldBounds = true; tile.outOfBoundsKill = true; }, addPlatform: function(y) { //If no y position is supplied, render it just outside of the screen if(typeof(y) == "undefined"){ y = -me.tileHeight; //Increase the players score me.incrementScore(); } //Work out how many tiles we need to fit across the whole screen var tilesNeeded = Math.ceil(me.game.world.width / me.tileWidth); //Add a hole randomly somewhere var hole = Math.floor(Math.random() * (tilesNeeded - 3)) + 1; //Keep creating tiles next to each other until we have an entire row //Don't add tiles where the random hole is for (var i = 0; i < tilesNeeded; i++){ if (i != hole && i != hole + 1){ this.addTile(i * me.tileWidth, y); } } }, initPlatforms: function() { var bottom = me.game.world.height - me.tileHeight, top = me.tileHeight; //Keep creating platforms until they reach (near) the top of the screen for(var y = bottom; y > top - me.tileHeight; y = y - me.spacing){ me.addPlatform(y); } }, createPlayer: function() { //Add the player to the game by creating a new sprite me.player = me.game.add.sprite(me.game.world.centerX, me.game.world.height - (me.spacing * 2 + (3 * me.tileHeight)), 'kisameSprite', 'stance/0.png'); me.player.scale.setTo(sizeOfPlayer); //Set the players anchor point to be in the middle horizontally me.player.anchor.setTo(0.5, 1.0); //Enable physics on the player me.game.physics.arcade.enable(me.player); //Make the player fall by applying gravity me.player.body.gravity.y = 2000; //Make the player collide with the game boundaries me.player.body.collideWorldBounds = true; //Make the player bounce a little me.player.body.bounce.y = 0.1; me.player.animations.add('attackMagic', Phaser.Animation.generateFrameNames('attackMagic/', 0, 24, '.png', 1), 7, false, true); me.player.animations.add('attack', Phaser.Animation.generateFrameNames('attack/', 0, 5, '.png', 1), 10, false, true); me.player.animations.add('left', Phaser.Animation.generateFrameNames('run/', 0, 4, '.png', 1), 10, true, true); me.player.animations.add('idle', Phaser.Animation.generateFrameNames('stance/', 0, 3, '.png', 1), 10, true, true); me.player.animations.add('right', Phaser.Animation.generateFrameNames('run/', 0, 4, '.png', 1), 10, true, true); me.player.animations.add('jump', Phaser.Animation.generateFrameNames('jump/', 0, 3, '.png', 1), 10, false, true); }, createScore: function(){ var scoreFont = "100px Arial"; me.scoreLabel = me.game.add.text((me.game.world.centerX), 100, "0", {font: scoreFont, fill: "#fff"}); me.scoreLabel.anchor.setTo(0.5, 0.5); me.scoreLabel.align = 'center'; }, incrementScore: function(){ me.score += 1; me.scoreLabel.text = me.score; }, }; function animateRunRight() { me.player.body.velocity.x = speedOfPlayerMovingRightLeft; if(facing != 'right') { me.player.scale.setTo(sizeOfPlayer, sizeOfPlayer); me.player.animations.play('right'); facing = 'right'; } } function animateRunLeft() { me.player.body.velocity.x = -speedOfPlayerMovingRightLeft; if(facing != 'left') { me.player.scale.setTo(-sizeOfPlayer, sizeOfPlayer); me.player.animations.play('left'); facing = 'left'; } } function jumpHasToOccur() { var jumButtonClicked = me.cursors.up.isDown || spaceBar.isDown; console.log("jumpButtonClicked == " + jumButtonClicked ); var alreadyOnFloor = me.player.body.onFloor() && me.game.time.now > jumpTimer; console.log("alreadyOnFloor == " + alreadyOnFloor); return jumButtonClicked && alreadyOnFloor; } function somethingWasPressed(keyCode) { if(keyEqualTo(keyCode, "a")) {animateAttack();} if(keyEqualTo(keyCode, "m")) {animateAttackMagic();} } function keyEqualTo(keyCode, key) { var equalKey = (keyCode.key == key); return equalKey; } function beIdle() { me.player.animations.play('idle'); if(facing == 'left') { me.player.frame = 5; } else { me.player.frame = 5; } facing = 'idle'; } function animateAttack() { me.player.animations.play('attack', 10, false, false); me.player.animations.currentAnim.onComplete.add(beIdle,this); } function animateAttackMagic() { me.player.animations.play('attackMagic', 3, false, false); me.player.animations.currentAnim.onComplete.add(beIdle,this); } I want player to collide with dynamically generated tiles, but it does not happen. The player just falls down after any animation. For example, is I run left or right the player runs, but falls down simultaneously. To make player and tiles collide I use that in update() function: me.game.physics.arcade.collide(me.player, me.platforms); So, help me, please to figure out what am I doing wrong, why collision between player and tiles does not occur when animation takes plays?