Jump to content

Dynamically changing an sprite's key


swissnetizen
 Share

Recommended Posts

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 by Samarthwiz
Link to comment
Share on other sites

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

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

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

 Share

  • Recently Browsing   0 members

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