pWEN

Members
  • Content Count

    9
  • Joined

  • Last visited

About pWEN

  • Rank
    Newbie
  • Birthday 10/25/1985

Contact Methods

  • Website URL
    http://www.owencm.com

Profile Information

  • Gender
    Male
  • Location
    Pittsburgh, PA
  1. Ah, so simple! Thank you, it works now! Here's my updated code, including calls to parent functions. var Bullet = function(game, key) { console.log("Bullet.prototype"); Phaser.Sprite.call(this, game, 0, 0, key); this.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST; this.anchor.set(0.5); this.checkWorldBounds = true; this.outOfBoundsKill = true; this.exists = false; this.tracking = false; this.scaleSpeed = 0; }; Bullet.prototype = Object.create(Phaser.Sprite.prototype); Bullet.prototype.constructor = Bullet; Bullet.prototype.fire = function(x, y, angle, speed, gx, gy) { console.log("Bullet.prototype.fire() - x: "+x+", y: "+y+", angle: "+angle+", speed: "+speed+", gx: "+gx+", gy: "+gy); gx = gx || 0; gy = gy || 0; this.reset(x, y); this.scale.set(1); this.game.physics.arcade.velocityFromAngle(angle, speed, this.body.velocity); this.angle = angle; }; Bullet.prototype.update = function() { //console.log("Bullet.prototype.update"); if (this.tracking) { this.rotation = Math.atan2(this.body.velocity.y, this.body.velocity.x); } if (this.scaleSpeed > 0) { this.scale.x += this.scaleSpeed; this.scale.y += this.scaleSpeed; } }; //////////////////////////////////////////////////////////// // PISTOL //////////////////////////////////////////////////////////// var BulletPistol = function(game) { console.log("Bullet.Pistol.prototype"); Bullet.call(this, game, 'img_BulletPistol'); }; BulletPistol.prototype = Object.create(Bullet.prototype); BulletPistol.prototype.constructor = BulletPistol; BulletPistol.prototype.fire = function(x, y, angle, speed, gx, gy) { Bullet.prototype.fire.call(this, x, y, angle, speed, gx, gy); }; BulletPistol.prototype.update = function() { Bullet.prototype.update.call(this); };
  2. Hello! I'm having a hell of a time trying to create different bullet types in my game (and this problem extends to different weapon types, enemy types, etc.). I know how to extend Sprite, and I have done so to make a base bullet class. However, I can't seem to figure out how to extend that class to make bullets that behave differently. For example: I have a weapon of type Pistol that will shoot bullets of type BulletPistol, and a weapon of RocketLauncher that will shoot bullets of type BulletRocket. The form is like a typical bullet, while the latter will start moving slowly, then accelerate. My thinking is that for the base Bullet class, I can put in all of the default behavior. For BulletPistol, and any other weapons that use this behavior, I merely pass in the sprite I want to use. For BulletRocket, I will need to override the update function (at least) to change how the rocket moves. Here is the code in my Bullet.js file: var Bullet = function(game, key) { console.log("Bullet.prototype"); Phaser.Sprite.call(this, game, 0, 0, key); this.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST; this.anchor.set(0.5); this.checkWorldBounds = true; this.outOfBoundsKill = true; this.exists = false; this.tracking = false; this.scaleSpeed = 0; }; Bullet.prototype = Object.create(Phaser.Sprite.prototype); Bullet.prototype.constructor = Bullet; Bullet.prototype.fire = function(x, y, angle, speed, gx, gy) { gx = gx || 0; gy = gy || 0; this.reset(x, y); this.scale.set(1); this.game.physics.arcade.velocityFromAngle(angle, speed, this.body.velocity); this.angle = angle; }; Bullet.prototype.update = function() { console.log("Bullet.prototype.update"); if (this.tracking) { this.rotation = Math.atan2(this.body.velocity.y, this.body.velocity.x); } if (this.scaleSpeed > 0) { this.scale.x += this.scaleSpeed; this.scale.y += this.scaleSpeed; } }; //////////////////////////////////////////////////////////// // PISTOL //////////////////////////////////////////////////////////// var BulletPistol = function(game) { console.log("Bullet.Pistol.prototype"); Bullet.call(game, 'img_BulletPistol'); }; BulletPistol.prototype = Object.create(Bullet.prototype); BulletPistol.prototype.constructor = BulletPistol; BulletPistol.prototype.fire = function(x, y, angle, speed, gx, gy) { // }; BulletPistol.prototype.update = function() { console.log("Bullet.Pistol.prototype.update"); }; Here are the error messages and console logs I get when I load my game: I'm not sure what I'm doing wrong, exactly. I went through 11 pages on this forum and several more on Google, but the closest things I could find to what I'm attempting were written in other languages. Any insight on how to extend an extended class is greatly appreciated!
  3. Ah, okay, that does make sense.
  4. Ah, it works! At first I rewrote my own for loops that built the levelStr string to be more like what you had, which didn't quite work. I copied your loop in and it still gave the data.trim() error. I don't understand why it can't work on a string... Very strange mystery, because I've verified every way possible that it is getting a string! Anyway, I changed my code to skip the parseCSV function entirely, passing in levelStr directly into game.cache.addTilemap. I also added the layer code you used, and removed the map.create() code. Now it displays the level with my tiles. Here is my finished function: drawTiles: function(){ console.log("drawTiles()"); var levelStr = ''; for (var c = 0; c < this.tilesPerColumn; c++) { for (var r = 0; r < this.tilesPerRow; r++) { var temp = levelArr[c][r].toString(); levelStr += temp; if (r < this.tilesPerRow-1) { levelStr += ','; } } if (c < this.tilesPerColumn-1) { levelStr += '\n'; } } //map is defined outside of this class game.cache.addTilemap('test', null, levelStr, Phaser.Tilemap.CSV); map = game.add.tilemap('test', TILE_SIZE, TILE_SIZE, this.levelWidth/TILE_SIZE, this.levelHeight/TILE_SIZE); map.addTilesetImage('tileimage', 'img_leveltiles_test', TILE_SIZE, TILE_SIZE); var layer = map.createLayer(0); layer.resizeWorld();}Thank you for the help!!!! Wish I knew what the deal was with that parsing function, but it's no problem as long as I don't need it. Now I can make this level generator more robust with layers and stuff.
  5. I did a check on levelStr before the parseCSV function, and typeof said string. I checked the data object inside the parseCSV function before it did data.trim() and it said string there, too. I tried doing data.trim() in Chrome's console, and it didn't throw any errors. I don't know what would make it different. I did a trace of the data object, and it was just like the output in my previous post. So then I changed the "data = data.trim()" line to this, and it worked: var trimmed = data.toString().trim();data = trimmed;yet if I leave the Phaser file as-is and just change it to pass in "levelStr.toString()" to parseCSV (instead of just "levelStr"), it throws the same error. Also, if I leave the new code in there, I don't see any tiles rendering, so even if I get that part working, I'm still doing something wrong, I guess.
  6. Ah, okay. I caught up on sleep so I can take a better look at this. I went through the phaser.js file, looking up each method, and making sure I'm passing the right stuff into them. (By the way, neither the documentation nor the hint comments in the code mention that Phaser.TilemapParser.parseCSV has a key as its first input.) Everything checks out now as far as I can tell. Now, however, the error I get is "data.trim is not a function", referring to line 92713 inside the parseCSV function. It seems it's not recognizing it as a string or something. Sounds similar to the issue this person was having, though I don't think they saw the error message: Manually adding and retrieving tilemap from cache Here's my current code: var levelStr = "";for(var i = 0; i < levelArr.length; i++){ levelStr += levelArr[i].toString(); if(i < levelArr.length-1) { levelStr += "\n"; }}//parseCSV: function (key, data, tileWidth, tileHeight)var parsed = Phaser.TilemapParser.parseCSV('sdfsdf', levelStr, TILE_SIZE, TILE_SIZE);//addTilemap: function (key, url, mapData, format)game.cache.addTilemap('test', null, parsed, Phaser.Tilemap.CSV);//tilemap: function (key, tileWidth, tileHeight, width, height)map = game.add.tilemap('test', TILE_SIZE, TILE_SIZE, this.levelWidth/TILE_SIZE, this.levelHeight/TILE_SIZE);//addTilesetImage: function (tileset, key, tileWidth, tileHeight, tileMargin, tileSpacing, gid)map.addTilesetImage('tileimage', 'img_leveltiles_test', TILE_SIZE, TILE_SIZE);//create: function (name, width, height, tileWidth, tileHeight, group)map.create('firstlevel', this.levelWidth/TILE_SIZE, this.levelHeight/TILE_SIZE, TILE_SIZE, TILE_SIZE);And here is the generated map string (levelStr) being passed into parseCSV (I put a breakpoint in the parseCSV method to grab the data object just as it gets passed in):
  7. Okay, I think I understand, but I still seem to be missing something. I'm trying this code now, in the same drawTiles function as before, but it's throwing an error, cannot read property 'addTilemap' of undefined on the "map" object. var levelStr = "";for(var i = 0; i < levelArr.length; i++){ levelStr += levelArr[i].toString(); if(i < levelArr.length-1) { levelStr += "\n"; }}var parsed = Phaser.TilemapParser.parseCSV('sdfsdf', levelStr, TILE_SIZE, TILE_SIZE);game.cache.addTilemap('test', parsed);map.addTilemap('test');map.addTilesetImage('tileimage', 'img_leveltiles_test', TILE_SIZE, TILE_SIZE);map.create('firstlevel', this.levelWidth/TILE_SIZE, this.levelHeight/TILE_SIZE, TILE_SIZE, TILE_SIZE);I'm sure you can tell by now, but I'm pretty new to Phaser, and I'm finding it difficult to figure out what's relevant solely from the documentation. :\
  8. I don't know if it's a good idea to put it in the preloader, as that currently happens before all of the level generation takes place. I guess I could move the level generation to happen in the preload section, but is that right way to do things? Can I maybe use a string of all 0s in game.load.tilemap and then update the tilemap later when some of those 0s become 1s? I tried using CSV also, but that wasn't working. Looking at how other people have done CSV, it seems they have line breaks or insert "\n" where needed to signify a new row. Would I need to do that when I just have one long comma separated string? I tried this instead, and it doesn't throw an error in getIndex anymore, but still doesn't display tiles: game.load.tilemap('tilemap', null, levelArr.toString(), Phaser.Tilemap.CSV);
  9. Hello, I'm using BSP to build an array for level generation. For now, the 2d array contains either a 0 or 1, for empty space or where a tile should be. I'm then trying to use the array as a string to pass into the tilemap function. However, none of the methods I've tried for generating the tilemap are working. The closest I've been able to get is throwing an error in phaser.js's getIndex function, saying that the "location" property is undefined. That seems like this issue, but that's been reported as fixed. I'm using version 2.4.4 of Phaser. I saw another thread that said 0 shouldn't be used, because when Phaser loops through the data, it only looks for things greater than 0. I changed it to use 1 for empty space, and 12 for a tile (just to pick something from my tilesheet), but that made no difference. Someone please tell me what I'm doing wrong! Here is the tilemap I slapped together for testing: Here's the code I'm using (stripping out all of the level generation stuff): var TILE_SIZE = 32;var WIDTH_RATIO = 0.45;var HEIGHT_RATIO = 0.45;var ITERATIONS = 4;var mainRoom = undefined;var levelArr = [];var map;var BSP2 = function (){ this.levelWidth = 0; this.levelHeight = 0; this.tilesPerRow = 0; this.tilesPerColumn = 0; this.roomTree = undefined; this.rooms = [];};BSP2.prototype = { init: function(levelWidth, levelHeight) { this.levelWidth = levelWidth; this.levelHeight = levelHeight; this.tilesPerRow = levelWidth/TILE_SIZE; this.tilesPerColumn = levelHeight/TILE_SIZE; mainRoom = new RoomContainer(0, 0, this.levelWidth, this.levelHeight); levelArr = new Array(this.tilesPerColumn); for(var i = 0; i < this.tilesPerColumn; i++) { levelArr[i] = new Array(this.tilesPerColumn); } for(var c = 0; c < this.tilesPerColumn; c++) { for(var r = 0; r < this.tilesPerRow; r++) { levelArr[c][r] = 1; } } }, preload: function() { //console.log("bsp2.js - preload()"); game.load.image('img_leveltiles_test', 'leveltiles_test.png'); }, create: function() { //console.log("bsp2.js - create()"); game.add.sprite(0, 0, 'bg_background'); ... this.drawTiles(); }, drawTiles: function() { game.load.tilemap('tilemap', null, [levelArr.toString()], Phaser.Tilemap.TILED_JSON); map = game.add.tilemap('tilemap'); map.addTilesetImage('tileimage', 'img_leveltiles_test', TILE_SIZE, TILE_SIZE); map.create('firstlevel', this.levelWidth/TILE_SIZE, this.levelHeight/TILE_SIZE, TILE_SIZE, TILE_SIZE); }};And this is what a typical array-to-string setup looks like: