swissnetizen Posted February 7, 2015 Share Posted February 7, 2015 (edited) I've got a game I'm working on which is like somewhat like snake. The sprite makes a tail behind it composed of several hundred points; I've decided that the best way to do this is to have one sprite where I change it's key dynamically. this.drawNewTailPoint = function (x, y) { var ctx = this.tail.key.ctx; ctx.beginPath(); ctx.arc(x, y, game.global.tailRadius, 0, 2 * Math.PI, false); ctx.fillStyle = this.color; ctx.fill(); };It executes multiple times (Several hundred times) however there is always only one point displayed.That point is always the first pointed drawn.I've also tried drawing a rectangle to see if the arc was the problem; problem persists. Edited February 8, 2015 by Samarthwiz Link to comment Share on other sites More sharing options...
rich Posted February 8, 2015 Share Posted February 8, 2015 I don't really get what you're trying to do in your code. You're modifying the context of a texture reference, which seems highly odd - exactly what type of texture is 'tail' using? If it's a normal image then you'd change it using Sprite.loadTexture. If it's a BitmapData then you should keep a reference to that somewhere else and modify it from that, not via .key. Link to comment Share on other sites More sharing options...
swissnetizen Posted February 8, 2015 Author Share Posted February 8, 2015 How would we reset a sprite's key?this.tail.key = this.newKeydoesn't work. Link to comment Share on other sites More sharing options...
neon Posted February 8, 2015 Share Posted February 8, 2015 Here is from my code:this.object.key = key;this.object.loadTexture(key, 0);So basically you need to call loadTexture after changing the key Link to comment Share on other sites More sharing options...
swissnetizen Posted February 8, 2015 Author Share Posted February 8, 2015 Just tried that; It doesn't solve the issue. Link to comment Share on other sites More sharing options...
rich Posted February 8, 2015 Share Posted February 8, 2015 You should never change the key directly, it won't do anything and it isn't meant to do anything. If you want to change the texture a sprite is using then use Sprite.loadTexture. On it's own, nothing else required. If that doesn't work you're not actually explaining the problem clearly enough I'm afraid. Link to comment Share on other sites More sharing options...
swissnetizen Posted February 8, 2015 Author Share Posted February 8, 2015 This is the full code for the player. There may be something which I missed which is causing the problem.game.Player = function (game, x, y, image, frame, add) { game.Actor.apply(this, arguments); this.points = []; this.speed = 5; //Invincibility(Can't die) this.invincible = true; setTimeout(function () { this.invincible = false; }.bind(this), 500); //"Tail" this.rect = game.make.bitmapData(1000, 1000); this.tail = game.add.sprite(0, 0, this.rect); //For adding points to tail this.drawNewTailPoint = function (x, y) { var ctx = this.rect.ctx; ctx.beginPath(); ctx.arc(x, y, game.global.tailRadius, 0, 2 * Math.PI, false); ctx.fillStyle = this.color; ctx.fill(); this.tail.loadTexture(this.rect, 0); }; //Updates at 1 cycle/frame this.update = function () { var speed = this.speed | 5; this.points.forEach(function (c) { if (Phaser.Rectangle.contains(this.body, c.x, c.y) && !this.invincible) { this.kill(); } }.bind(this)); this.points.push({x: this.x, y: this.y}); this.drawNewTailPoint(this.x, this.y); }};game.Player.prototype = game.Actor.prototype; Link to comment Share on other sites More sharing options...
swissnetizen Posted February 9, 2015 Author Share Posted February 9, 2015 I realised I was using an old version of Phaser. I've just upgraded to the new one and now it works. Link to comment Share on other sites More sharing options...
Recommended Posts