Flixel Forums

development => help => Topic started by: NateTheGreatt on Wed, Nov 17, 2010

Title: using native sprite and graphic functions in Flixel?
Post by: NateTheGreatt on Wed, Nov 17, 2010
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?  ???
Title: Re: using native sprite and graphic functions in Flixel?
Post by: photonstorm 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)

Title: Re: using native sprite and graphic functions in Flixel?
Post by: NateTheGreatt 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.
Title: Re: using native sprite and graphic functions in Flixel?
Post by: cai 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.
Title: Re: using native sprite and graphic functions in Flixel?
Post by: Richard Kain 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.
Title: Re: using native sprite and graphic functions in Flixel?
Post by: cai 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.
Title: Re: using native sprite and graphic functions in Flixel?
Post by: photonstorm 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 :)
Title: Re: using native sprite and graphic functions in Flixel?
Post by: NateTheGreatt 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