Author Topic: using native sprite and graphic functions in Flixel?  (Read 1671 times)

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
I'm trying to implement mouse gestures into my game right now. I started off simple, just trying to enable the mouse to draw on the screen when the mouse is held down.

For some reason when I have this line in the create() function of the state:

g.graphics.lineStyle(1, 0x000000);

it breaks the state. Nothing appears and it seems frozen. Any ideas why this is happening?  ???
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: using native sprite and graphic functions in Flixel?
« Reply #1 on: Wed, Nov 17, 2010 »
What is g ? As the State extends Sprite it should just be graphics (or this.graphics if you want more clarity)

http://www.photonstorm.com



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

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: using native sprite and graphic functions in Flixel?
« Reply #2 on: Wed, Nov 17, 2010 »
What is g ? As the State extends Sprite it should just be graphics (or this.graphics if you want more clarity)



Sorry, g is defined as follows: private var g:Sprite;

That should work, no?

Edit: Here's the entire file:
Code: [Select]
package

{

import flash.display.*;
import org.flixel.*;

public class MainMenu extends FlxState
{

private var drawing:Boolean;

private var g:Sprite;

override public function create():void
{
bgColor = 0xffffffff;
var t:FlxText;
t = new FlxText(0,FlxG.height/2-10,FlxG.width,"Mouse Gestures Test");
t.size = 16;
t.alignment = "center";
t.color = 0xff000000;
add(t);
t = new FlxText(FlxG.width/2-50,FlxG.height-20,100,"click to play");
t.alignment = "center";
t.color = 0xff000000;
add(t);

drawing = false;

g.graphics.lineStyle(2, 0xFF0000,1);
addChild(g);

FlxG.mouse.show();
}



override public function update():void
{
super.update();

/*if (FlxG.mouse.pressed())
drawing = true;
else
drawing = false;*/

/*if (drawing)
g.graphics.lineTo(mouseX, mouseY);*/


/*if(FlxG.mouse.justPressed())
{
//FlxG.mouse.hide();
FlxG.state = new PlayState();
}*/
}

}

}

I have the things in update() commented out for testing purposes.
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

cai

  • Contributor
  • ****
  • Posts: 465
  • Karma: +0/-0
  • the illest of villains
    • View Profile
    • Brandon Cash
Re: using native sprite and graphic functions in Flixel?
« Reply #3 on: Wed, Nov 17, 2010 »
You've never instantiated g, so it's not yet a Sprite.  You should be getting an error in the debug player.
Follow me on Twitter | Come join us at #flixel on irc.freenode.net!

Richard Kain

  • Active Member
  • ***
  • Posts: 231
  • Karma: +0/-0
    • View Profile
Re: using native sprite and graphic functions in Flixel?
« Reply #4 on: Wed, Nov 17, 2010 »
not to mention the fact that you are using Flash drawing functions, while Flixel has almost nothing at all to do with Flash's default drawing functions. I'm not certain of how this would behave inside of an FlxState. I'm not even certain if FlxStates get added to the DisplayList. If they don't, then your attempt would never work.

For instantiating the empty g Sprite, you would do this...
Code: [Select]
g = new Sprite();
g.width = <desired width>;
g.height = <desired height>;

Then you would make the addChild(g) call.

cai

  • Contributor
  • ****
  • Posts: 465
  • Karma: +0/-0
  • the illest of villains
    • View Profile
    • Brandon Cash
Re: using native sprite and graphic functions in Flixel?
« Reply #5 on: Wed, Nov 17, 2010 »
Well, there's no reason why it wouldn't work inside flixel.  It might not get displayed correctly, but that's easily overcome by using .draw() to render it onto FlxG.buffer.
Follow me on Twitter | Come join us at #flixel on irc.freenode.net!

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: using native sprite and graphic functions in Flixel?
« Reply #6 on: Wed, Nov 17, 2010 »
FlxState just extends a Sprite, and is added onto the Display List. So you can mix and match Flixel objects (like FlxSprite, FlxText, etc) and standard display objects (Sprites, MovieClips, etc) quite happily.

Code: [Select]
package  
{
import flash.display.Sprite;
import org.flixel.*;

public class Test1 extends FlxState
{
private var test:FlxText;
private var circle:Sprite;

public function Test1()
{
super();

test = new FlxText(0, 0, 200, "test 1");

circle = new Sprite;
circle.graphics.beginFill(0xFF0080, 0.5);
circle.graphics.drawCircle(40, 40, 80);
circle.graphics.endFill();

add(test);

// Doesn't matter the order, addChild will ALWAYS appear over the top of add()
addChild(circle);
}

}

}

There are some important things to remember:

1) It doesn't matter if you do addChild before or after an add() call, they will ALWAYS appear above the Flixel objects.

2) If you have set the game to be zoomed in your FlxGame, then display objects added in the FlxState will be zoomed by that amount too.

3) Flixel will not clear up display objects for you. So if you add a clip with lots of animation and events firing, and don't clear it properly, it can stay in memory if references exist.

4) Coordinates of display objects in the FlxState are screen orientated, which is not the same as the game-world values which FlxSprites can use.

5) Forget collision and other Flixel commands like that on display objects :)
« Last Edit: Wed, Nov 17, 2010 by photonstorm »
http://www.photonstorm.com



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

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: using native sprite and graphic functions in Flixel?
« Reply #7 on: Wed, Nov 17, 2010 »
You've never instantiated g, so it's not yet a Sprite.  You should be getting an error in the debug player.

LMAO oh god I'm so embarrassed  :-[

thanks  :D
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery