piobug

Members
  • Content count

    84
  • Joined

  • Last visited

About piobug

  • Rank
    Advanced Member

Profile Information

  • Gender
    Not Telling

Recent Profile Visitors

789 profile views
  1. Thanks, that helped! Here's the code that is working for me var distanceFromTarget = 10; var targetVelocity; var pointBehindTarget = new Phaser.Point(); targetVelocity = new Phaser.Point(target.body.velocity.x, target.body.velocity.y); //make a copy of the velocity vector targetVelocity.multiply(-1,-1); //invert its direction targetVelocity.normalize(); //set its length to 1 targetVelocity.multiply(distanceFromTarget,distanceFromTarget); //scale it to the desired length pointBehindTarget = Phaser.Point.add(target.position,targetVelocity); //create a new point
  2. Hi, how do you get the velocity vector of a sprite? sprite.body.velocity is phaser point or as a vector, going from the 0,0 to the sprite. What I need is a velocity vector going from the sprite onward. I need then to multiply by -1 and add it to the sprite position to get a point behind it like this image Thanks
  3. Hi all, If I set tiles to collide only on top, then when I swap tiles with "putTile", "removeTile" or "replace", any collision data previously set is lost. Is this working as intended? (Assuming that all tiles are in the "blocksArray" array) var playState = { create: function() { //create and save all Tiled layers into an array this.allLayers = []; for(var i = 0; i < this.map.layers.length; i++) { //crate all layers var layer = this.map.createLayer(''+ this.map.layers[i].name +''); //save all created layers this.allLayers.push(layer); } //search all layers for(var i = 0; i < this.allLayers.length; i++) { //set special collision for all tiles in the blocksArray if(blocksArray.length > 0) { this.setBlockCollision(this.allLayers[i], blocksArray, { top: true, bottom: false, left: false, right: false }); } } } update: function() { for(var i = 0; i < this.allLayers.length; i++) { game.physics.arcade.collide(this.player, this.allLayers[i], function(player, block){ this.contactWithPlatform(player, block, this.allLayers[i]);}, null, this); } } //------- ISSUES START HERE -------- // contactWithPlatform: function (player, block, platformLayer) { //method 1 //this.map.removeTile(block.index); //this.map.putTile(block.index, block.x, block.y); //method2 this.map.replace(block.index,block.index++, 1, 1, 1, 1); } //------- ISSUES EDS HERE -------- // //http://thoughts.amphibian.com/2015/11/single-direction-collision-for-your.html setBlockCollision: function (mapLayer, idxOrArray, dirs) { var mFunc; // tile index matching function if (idxOrArray.length) { // if idxOrArray is an array, use a function with a loop mFunc = function(inp) { for (var i = 0; i < idxOrArray.length; i++) { if (idxOrArray[i] === inp) { return true; } } return false; }; } else { // if idxOrArray is a single number, use a simple function mFunc = function(inp) { return inp === idxOrArray; }; } // get the 2-dimensional tiles array for this layer var d = mapLayer.map.layers[mapLayer.index].data; for (var i = 0; i < d.length; i++) { for (var j = 0; j < d[i].length; j++) { var t = d[i][j]; if (mFunc(t.index)) { t.collideUp = dirs.top; t.collideDown = dirs.bottom; t.collideLeft = dirs.left; t.collideRight = dirs.right; t.faceTop = dirs.top; t.faceBottom = dirs.bottom; t.faceLeft = dirs.left; t.faceRight = dirs.right; } } } },
  4. piobug

    How to organise a map's tiles into groups?

    Thanks Casey. I've tried the first options, and it works. 1. Tiled setup Put each platform on a separate Tiled layer. The layer should have a custom boolean property called "complete" Every tile should have a boolean custom property called "touched" 2. The code create: function() { //create and save all Tiled layers this.allLayers = []; for(var i = 0; i < this.map.layers.length; i++) { //crate all layers var layer = this.map.createLayer(''+ this.map.layers[i].name +''); //save all created layers this.allLayers.push(layer); } //set collision for the layers created for(var i = 0; i < this.allLayers.length; i++) { this.map.setCollisionBetween(0,1000, true, this.allLayers[i]); } }, update: function() { for(var i = 0; i < this.allLayers.length; i++) { //pass the actual layer as an extra argument, because the first accourance is a tile game.physics.arcade.collide(this.player, this.allLayers[i], function(player, tile){ this.touchTile(player, tile, this.allLayers[i]);}, null, this); } }, touchTile: function(player,tile,layer) { if(tile.properties.touched == false) { tile.properties.touched == true; } this.checkPlatform(player,tile,layer); }, checkPlatform: function (player,tile,layer) { //if not all tiles have been touched if(tile.layer.properties.complete == false) { //get all tiles in the layer var platformTiles = layer.getTiles(layer.x,layer.y,layer.width, layer.height,true,true); var touchedTiles = []; //look for touched tiles in the layers and save them in an array for(var i = 0; i < platformTiles.length; i++) { if(platformTiles[i].properties.touched == true) { touchedTiles.push(platformTiles[i]); } } //check if the all tiles have been touched if(touchedTiles.length === platformTiles.length) { for(var i = 0; i < platformTiles.length; i++) { platformTiles[i].index++; } //all tiles have been touched tile.layer.properties.complete = true; } } },
  5. Hi all, I need advice on which approach is best for my problem: Setup: 1. I'm using Tiled and tiles sets to create platforms for the player to jump on 2. Each platform is made of 32x32 tiles and can have any shape, like in tetris. (line-shaped, "L"-shaped, 2x2 block, and so on) 3. A level can have up to 50 platforms Desired outcome: I want is to know when the player has touched every tile of a platform. E.g. a platform has 5 tiles and after the player collided with all of them I want to call a function. What would be the best way of doing this? Solutions considered: 1. Have one layer per platform and get all tiles in each layer once the player touches the first tile. Then have a boolean on each tile, and substract the total touched tiles from the total tiles in the layer. When zero, call the function 2. Have some function that looks for adjacent tiles to the one touched by the player. Are there any better solutions? Thanks
  6. piobug

    Creating A Custom Player

    @fedora any success with this problem?
  7. piobug

    Character Customization Idea

    Anyone has been successful with any of those implementations and can post some code, please?
  8. Thanks! I didn't know about onComplete.addOnce. Now it works. Here's the full working code: Enemy.Shaker = function (game,player) { game.time.events.add(1000, this.show, this); }; Enemy.Shaker.prototype = Object.create(Phaser.Sprite.prototype); Enemy.Shaker.prototype.constructor = Enemy.Shaker; Enemy.Shaker.prototype.show = function() { this.visible = true; this.animations.play('isEnemy').onComplete.addOnce(function() { this.game.camera.shake(0.05, 1000); game.time.events.add(1200, this.hide, this); }, this); console.log('show'); }; Enemy.Shaker.prototype.hide = function() { this.visible = false; game.time.events.add(2000, this.show, this); console.log('hide'); };
  9. Thanks. It makes sense but I can't figure out how to only add it once because I don't see where it is added twice.
  10. Hi all, I want to show and hide a sprite with timers. With this code it seems that the timers are called multiple times every time and this causes the game to freeze and eventually crash. Is there a way to add timers once and them just call them when needed? Thanks Enemy.Shaker = function (game,player) { game.time.events.add(1000, this.show, this); }; Enemy.Shaker.prototype = Object.create(Phaser.Sprite.prototype); Enemy.Shaker.prototype.constructor = Enemy.Shaker; Enemy.Shaker.prototype.show = function() { this.visible = true; this.animations.play('isEnemy').onComplete.add(function() { this.game.camera.shake(0.05, 1000); game.time.events.add(1200, this.hide, this); }, this); console.log('show'); }; Enemy.Shaker.prototype.hide = function() { this.visible = false; game.time.events.add(2000, this.show, this); console.log('hide'); };
  11. piobug

    How to double jump with phaser ?

    Game Mechanic Explorer's website has a double jump example in Phaser https://gamemechanicexplorer.com/#platformer-5
  12. Do you mean like this? init: function(levelData) { this.game.levelData = levelData; }, create: function() { this.enemy = new Enemy[this.game.levelData.enemy] (game, this.player); this.add.existing(this.enemy); } I get a " Enemy[this.game.levelData.enemy] is not a constructor" error message
  13. Hi all, I have the game object "levelData" in "menu.js", that decides which assets will be loaded per level. In "play.js" I manage to load a different map per level, but I can't figure out how to create a different enemy per level using the new constructor. Thanks var menuState = { init: function() { this.levelData = { 1: { enemy: 'teacher', map: 'teacher', timeScore: '' }, 2: { enemy: 'seeker', map: 'base', timeScore: '' } }; }, //other stuff... var playState = { init: function(levelData) { this.levelData = levelData; }, create: function() { //this works this.map = game.add.tilemap(this.levelData.map); //this ofc doesn't this.enemy = new Enemy[this.levelData.enemy] (game, this.player); this.add.existing(this.enemy); }, }; var Enemy = {}; Enemy.Teacher = function (game,target) { //relevant code }; Enemy.Teacher.prototype = Object.create(Phaser.Sprite.prototype); Enemy.Teacher.prototype.constructor = Enemy.Teacher; Enemy.Seeker = function (game,target) { //relevant code }; Enemy.Seeker.prototype = Object.create(Phaser.Sprite.prototype); Enemy.Seeker.prototype.constructor = Enemy.Seeker;