• Content Count

  • Joined

  • Last visited

About cococohen

  • Rank

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. cococohen

    Adding visibility flashlight affect to a mask

    Ended up creating a different visibility polygon called maskGraphics, adding a 'blackness' sprite over the whole game, adding another sprite object that had the entire picture of the map itself over the whole thing, then made the visibility polygon the mask of the map sprite. It works! create() { //Create game set up through tiled state by calling super; this.lightAngle = Math.PI/2; this.numberOfRays = 600; this.rayLength = 300; this.wallsBitmap =,3200); this.wallsBitmap.draw('mapbitmap'); this.wallsBitmap.update();,0,this.wallsBitmap); this.maskGraphics =,0); this.blackness =,0,'blackness'); this.blackness.alpha = 0.7; this.mapSprite =,0,'map'); ... } update () { ... let mouseAngle = Math.atan2(,; this.maskGraphics.clear(); this.maskGraphics.lineStyle(2, 0xffffff, 1); this.maskGraphics.beginFill(0xffff00, 0.5); this.maskGraphics.moveTo(this.currentPlayerSprite.x, this.currentPlayerSprite.y); for(var i = 0; i<this.numberOfRays; i++){ //this.maskGraphics.moveTo(this.currentPlayerSprite.x,this.currentPlayerSprite.y); let rayAngle = mouseAngle-(this.lightAngle/2)+(this.lightAngle/this.numberOfRays)*i let lastX = this.currentPlayerSprite.x; let lastY = this.currentPlayerSprite.y; for(let j= 1; j<=this.rayLength;j+=1){ var landingX = Math.ceil(this.currentPlayerSprite.x-(2*j)*Math.cos(rayAngle)); var landingY = Math.ceil(this.currentPlayerSprite.y-(2*j)*Math.sin(rayAngle)); if(this.wallsBitmap.getPixel32(landingX,landingY)===0){ lastX = landingX; lastY = landingY; } else{ this.maskGraphics.lineTo(lastX,lastY); break; } } this.maskGraphics.lineTo(lastX,lastY); } this.maskGraphics.lineTo(this.currentPlayerSprite.x,this.currentPlayerSprite.y); this.maskGraphics.endFill(); this.mapSprite.mask = this.maskGraphics; } However, I still haven't been able to find a way to hide other entities (other players/sprites) when they are not in the visibility polygon. Thanks for your help!
  2. I have a Phaser game created and I am able to create an updated visibility graphic object while my sprite moves. However, like in this example, and every other I have found online:, they are using an image of the thing you want to see as the 'background', and simply .mask against the visibility graphic. In my case, I do not have an image of the map as it is created through a tilemap generator, so my current code basically shows the opposite of what I am looking for: For this example, I created a test box in the bottom right of the screen that has nothing to do with the real map (I'll add the correct walls later to the visibility algorithm). I want the part that is not white to be blacked out, and the whited part to be crystal clear. I have tried creating a sprite of just black, and a sprite of just transparent, and had them .mask = visibility graphic. But this just overwrites everything else. I have taken out the most important part of the code for reference: preload () { //load assets that are specific for this level'blackness', '../../assets/images/blackness.jpg');'transparent', '../../assets/images/transparent.png'); //'yellowish', '../../assets/images/yellowish.png') } create () { this.blackness =,0,'blackness'); //this.blackness.alpha = 0.8; //this.transparent =,0,'transparent'); this.lightCanvas =,0); } update() { let visibility = this.createLightPolygon(this.currentPlayerSprite.x, this.currentPlayerSprite.y); this.lightCanvas.clear(); this.lightCanvas.beginFill(0xffffff,0.5); console.log('visibility',visibility) this.lightCanvas.moveTo(visibility[0][0],visibility[0][1]); for(let i=1;i<=visibility.length;i++){ this.lightCanvas.lineTo(visibility[i%visibility.length][0],visibility[i%visibility.length][1]); } //this.transparent.mask = this.lightCanvas; this.lightCanvas.endFill(); }