Author Topic: How to draw stuff into a buffer/Sprite? setPixel()  (Read 2230 times)

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Hi,

I'm trying to make some effect trails for my bullets, after diggin around I came up with this:

In my Main Playstate I made this new sprite:

Code: [Select]
EffectLayer = new FlxSprite();
EffectLayer.makeGraphic(FlxG.width, FlxG.height, 0, true);
EffectLayer.setOriginToCorner();
EffectLayer.scrollFactor.x = EffectLayer.scrollFactor.y = 0;
EffectLayer.blend = "screen";



In my bullet class:

Code: [Select]
override public function update():void
{
(FlxG.state as PlayState).EffectLayer.pixels.setPixel(this.x, this.y, 0xFF333333);
}


but nothing happens :(
Effect layer is there (see it in debug) and drawline function works on it but I cant get the setPixels to do anything??
« Last Edit: Thu, Jan 19, 2012 by c023-DeV »
If you aint got no dedication, you won't get no education!

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #1 on: Thu, Jan 19, 2012 »
Copy pixels out to a temporary bitmapData, do your calls on it, then set it back again:

Code: [Select]
tempData = layer.pixels;
tempData.setPixel(magic here);
layer.pixels = tempData;
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #2 on: Thu, Jan 19, 2012 »
hmm... so I put this into my bullets update:

override public function update():void
      {
         
         var tempData:BitmapData = (FlxG.state as PlayState).EffectLayer.pixels;
         tempData.setPixel(this.x, this.y, 0xFF333333);
         (FlxG.state as PlayState).EffectLayer.pixels = tempData;
}

didn't work, I might be missing something crucial, as I am not a programmer =(

I've read about having to put that into a draw() of a sprite ans not to the update, but that didnt work either =(

This was my starting point:
http://forums.flixel.org/index.php/topic,1602.msg10350.html#msg10350   
If you aint got no dedication, you won't get no education!

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #3 on: Thu, Jan 19, 2012 »
You are probably missing super.update() from that.
Also if that doesn't work try setting the sprite to dirty after - EffectLayer.dirty = true
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #4 on: Thu, Jan 19, 2012 »


still nothing, I read something about adding that stuff to draw() somewhere but that didnt work either, I feel so stupid right now =(

But is the overall thought ok?

here is my Bullet class:

Code: [Select]
package
{
import flash.display.BitmapData;
import org.flixel.*;
import org.flixel.plugin.photonstorm.FlxVelocity;
import flash.display.BitmapData;
public class WpnBlaster extends FlxSprite
{

public var speed:Number;
//internal var _hazzardsmoke:FlxEmitter;

public function WpnBlaster()
{
super();
loadRotatedGraphic(ZooAssetReg.wpn_blaster01PNG, 16, -1, true, true);

width = 2;
height = 2;
offset.x = 1;
offset.y = 1;

speed = 835;

}

override public function update():void
{
//super.update();
var tempData:BitmapData = (FlxG.state as PlayState).EffectLayer.pixels;
         tempData.setPixel(this.x, this.y, 0xfff);
         (FlxG.state as PlayState).EffectLayer.pixels = tempData;
(FlxG.state as PlayState).EffectLayer.dirty = true;
super.update();


if(this.y < LvLPK01.boundsMinY)
{
alive = false;
solid = false;
}

if(!alive)
{
//if(finished)
exists = false;
}
else if(touching)
kill();
}

override public function kill():void
{
if(!alive)
return;
velocity.x = 0;
velocity.y = 0;

FlxG.play(ZooAssetReg.audio_sfx_punch01MP3);

alive = false;
solid = false;
//play("poof");
}

public function launch(Location:FlxPoint):void
{
//FlxG.play(SndShoot,0.5);

super.reset(Registry.player.x+4-width/2,Registry.player.y+6-width/2);
angle = (FlxG.state as PlayState).playerUpper.playerAimAngle + 90;
FlxVelocity.moveTowardsMouse(this, speed,0)
solid = true;
FlxG.play(ZooAssetReg.audio_sfx_blastershot01MP3);

}
}
}

and as said in my Playstate this:

Code: [Select]
EffectLayer = new FlxSprite();
EffectLayer.makeGraphic(FlxG.width, FlxG.height, 0, true);
EffectLayer.setOriginToCorner();
EffectLayer.scrollFactor.x = EffectLayer.scrollFactor.y = 0;
EffectLayer.blend = "screen";

(and I add the EffectLayer with add())
If you aint got no dedication, you won't get no education!

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #5 on: Thu, Jan 19, 2012 »
Try this:

Code: [Select]
tempData.setPixel32(this.x, this.y, 0xffff0000);
Uses the alpha channel to set a red pixel. See if that works first, then change the colour after.
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #6 on: Thu, Jan 19, 2012 »
No =(
If you aint got no dedication, you won't get no education!

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #7 on: Thu, Jan 19, 2012 »
The code is fine, so I suspect something else is wrong outside of this class.
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #8 on: Thu, Jan 19, 2012 »
hmm,

funny but for testing: drawLine works onto that layer, I don't get it...
If you aint got no dedication, you won't get no education!

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #9 on: Thu, Jan 19, 2012 »
AAAH!!! I think I've got it!

Scrolling... world and screen coordinates, my old freinds!!!

now I just need to figure out how to get the right ones...

Ok:

tempData.setPixel32(this.x - (FlxG.state as PlayState).camera.scroll.x, this.y - (FlxG.state as PlayState).camera.scroll.y, 0xffff0000);

Iiiek* no not quite there yet... the positioning isnt right yet but that was the issue

Another Edit:

I had to take out the EffectLayer.setOriginToCorner(); then it works... now to get it to fade out *Yipiee*

Thanks Photon for your mental support!
« Last Edit: Thu, Jan 19, 2012 by c023-DeV »
If you aint got no dedication, you won't get no education!

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Re: How to draw stuff into a buffer/Sprite? setPixel()
« Reply #10 on: Thu, Jan 19, 2012 »
Any tip on the fading out?

Code: [Select]
EffectLayer.framePixels.colorTransform(new Rectangle(0, 0, FlxG.width, FlxG.height), new ColorTransform(1, 1, 1, 0.95,0,0,0,-1));
does fade them out but doesn't clear out the data...

this works a bit but seems quite heavy on performance:

Code: [Select]
if (effect_timer.hasExpired)
{
EffectLayer.fill(0x00000000);
effect_timer.start();
}
If you aint got no dedication, you won't get no education!