Greetings, The hope is to give imitation event listeners to individual phx.World.bodies by putting a sprite on top of an existing body so that when the shape moves about in space the sprite follows on top, and can then be given a mouseEvent listener so that a person can grab something or hover over it and have it glow etc...fun stuff to play with. The idea is straight from the google.code physaxe page. I'm having trouble putting it together because of confusion as to how to refresh a sprite added onto the stage to follow something in a phx.world. I have successfully mapped the sprite to the initial x and y values of a phx.world.body but am having trouble understanding how to keep it refreshed. Which is basically saying I've made no progress at all! I considered using a Timer and then an updateAfterEvent. The other idea was sticking it in the loop that redraws the world. I was curious to see if anyone else has had any success interest in this. Any ideas, suggestions etc are greatly appreciated. Thanks, Dustin Hurt -- haXe - an open source web programming language http://haxe.org |
Cheesy code to follow: On Wednesday 22 October 2008 09:21:32 Dustin wrote: > Greetings, > > The hope is to give imitation event listeners to individual > phx.World.bodies by putting a sprite on top of an existing body so that > when the shape moves about in space the sprite follows on top, and can then > be given a mouseEvent listener so that a person can grab something or hover > over it and have it glow etc...fun stuff to play with. The idea is straight > from the google.code physaxe page. > > I'm having trouble putting it together because of confusion as to how to > refresh a sprite added onto the stage to follow something in a phx.world. > I have successfully mapped the sprite to the initial x and y values of a > phx.world.body but am having trouble understanding how to keep it > refreshed. Which is basically saying I've made no progress at all! > I considered using a Timer and then an updateAfterEvent. The other idea was > sticking it in the loop that redraws the world. I was curious to see if > anyone else has had any success interest in this. > > Any ideas, suggestions etc are greatly appreciated. > > Thanks, > Dustin Hurt -- haXe - an open source web programming language http://haxe.org |
In reply to this post by dustin-2
import flash.display.MovieClip; import flash.display.Graphics; import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.utils.Timer; import flash.display.BlendMode; import haxe.Timer; class Test2 { static var world : phx.World; public static var inst : Test2; var root : flash.display.MovieClip; var updatetimer:Timer; function new(root){ this.root = root; var stage = root.stage; // updatetimer = new Timer( 10, 0 ); // T=10ms =f= // updatetimer.addEventListener(TimerEvent.TIMER, update); //updatetimer.stop(); // define the size of the world var size = new phx.col.AABB(-1000,-1000,1000,1000); // create the broadphase : this is the algorithm used to optimize collision detection var bf = new phx.col.SortedList(); // initialize the world world = new phx.World(size,bf); var b1 = new phx.Body(210,-50); b1.addShape( new phx.Circle(20,new phx.Vector(0,0)) );
// create one 30 radius circle at x=200,y=250 var b2 = new phx.Body(200,250); b2.addShape( new phx.Circle(20,new phx.Vector(0,0)) ); // create one 20x20 box body at x=100,y=270 var b3 = new phx.Body(100,270); b3.addShape( phx.Shape.makeBox(20,20) ); // add the created bodies to the world world.addBody(b1); world.addBody(b2); world.addBody(b3);
var bPosY = b3.y; var bPosX = b3.x; var b6place = new phx.Vector(root.stage.stageWidth,root.stage.stageHeight); b6place.x += 50; b6place.y /= 9; // var v = new phx.Vector( root.mouseX - pos.x, root.mouseY - pos.y ); // var k = 95 / v.length(); // v.x *= k; // v.y *= k; var r:MovieClip = new MovieClip(); r.graphics.beginFill(0xaaaaff); r.graphics.drawRect(0,0,10,10); root.stage.addChild(r); r.x = bPosX / 2; r.y = bPosY / 2;
// creates a 380x50 box at x=0,y=280 var floor = phx.Shape.makeBox(380,50,0,280); // the floor is static, it can't move world.addStaticShape(floor); var stone1 = phx.Shape.makeBox(30,30,350,250); world.addStaticShape(stone1); var stone2 = phx.Shape.makeBox(30,30,0,250); world.addStaticShape(stone2); // setup gravity world.gravity = new phx.Vector(0,0.3);
// for every frame, call the loop method stage.addEventListener(flash.events.Event.ENTER_FRAME,loop); stage.addEventListener(flash.events.MouseEvent.MOUSE_DOWN,toFire); } private function update(evt:TimerEvent) {
evt.updateAfterEvent(); } function toFire( e : flash.events.MouseEvent ) { fireBall(); } public function fireBall() { var pos = new phx.Vector(root.stage.stageWidth,root.stage.stageHeight); pos.x += 50; pos.y /= 9; var v = new phx.Vector( root.mouseX - pos.x, root.mouseY - pos.y ); var k = 95 / v.length(); v.x *= k; v.y *= k; var b = new phx.Body(0,0); b.set(pos,20,v,13);
// var rnd = Math.random()*100; // if(rnd < 35){ // b.addShape( phx.Shape.makeBox(20,20,new phx.Material(0.0, 1, 5)) ); // }else{ b.addShape( new phx.Circle(10, new phx.Vector(0,0)) ); // } //b.addShape( phx.Shape.makeBox(20,20,new phx.Material(0.0, 1, 5)) ); //b.addShape( new phx.Circle(10, new phx.Vector(0,0)) ); world.addBody(b); trace("fired!" + b.y); } function loop(_) { // update the world world.step(0.8,20); // clear the graphics var g = flash.Lib.current.graphics; g.clear(); // draw the world var fd = new phx.FlashDraw(g); fd.drawCircleRotation = true; fd.drawWorld(world); } static function main() { flash.Lib.current.stage.scaleMode = flash.display.StageScaleMode.NO_SCALE; inst = new Test2(flash.Lib.current); trace("click to fire"); } } -- haXe - an open source web programming language http://haxe.org |
In reply to this post by dustin-2
Reply to myself for the third time. Please accept my apologies. I accidentally sent a test version I was doinking with that makes no sense. At any rate--any ideas are appreciated. This is the last unnecessary email. On Wednesday 22 October 2008 09:21:32 Dustin wrote: > Greetings, > > The hope is to give imitation event listeners to individual > phx.World.bodies by putting a sprite on top of an existing body so that > when the shape moves about in space the sprite follows on top, and can then > be given a mouseEvent listener so that a person can grab something or hover > over it and have it glow etc...fun stuff to play with. The idea is straight > from the google.code physaxe page. > > I'm having trouble putting it together because of confusion as to how to > refresh a sprite added onto the stage to follow something in a phx.world. > I have successfully mapped the sprite to the initial x and y values of a > phx.world.body but am having trouble understanding how to keep it > refreshed. Which is basically saying I've made no progress at all! > I considered using a Timer and then an updateAfterEvent. The other idea was > sticking it in the loop that redraws the world. I was curious to see if > anyone else has had any success interest in this. > > Any ideas, suggestions etc are greatly appreciated. > > Thanks, > Dustin Hurt -- haXe - an open source web programming language http://haxe.org |
In reply to this post by dustin-2
Hi,
I think the basic idea would be the create the sprites, and each sprite would "own" a "Body". So each sprite adds its body to the world, and you add each sprite the the stage. Then, after you have updated the physics in "enter frame", you would loop through each of your bodies and update the graphics position/orientation from their "body" data. You would not call "FlashDraw". The only complication is setting up the callback to remove the Sprite when the body is removed. Hugh -- haXe - an open source web programming language http://haxe.org |
Cool. Thanks so much for your reply. This sets me off in a very different direction. I'll spend some time playing around. Thanks again, Dustin On Wednesday 22 October 2008 09:53:21 Hugh Sanderson wrote: > Hi, > I think the basic idea would be the create the sprites, and each > sprite would "own" a "Body". So each sprite adds its body > to the world, and you add each sprite the the stage. > > Then, after you have updated the physics in "enter frame", > you would loop through each of your bodies and update > the graphics position/orientation from their "body" data. > > You would not call "FlashDraw". > > The only complication is setting up the callback to remove > the Sprite when the body is removed. > > Hugh -- haXe - an open source web programming language http://haxe.org |
In reply to this post by Gamehaxe
For anyone who comes across this later. The issue I was having had to do with properly declaring for my main class my body and sprite, and then adding the sprite correctly to the stage. After setting the sprite's x and y to the particular body's x and y in the same loop that updates the world it works exactly as described on the google.code page for physaxe. This probably will only be helpful to people new to haxe coming from flash but it's a lot of fun regardless. import flash.display.MovieClip; import flash.display.Graphics; import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Rectangle; class Test2 { static var world : phx.World; public static var inst : Test2; var root : flash.display.MovieClip; private var cursor:Sprite; var mySprite:Sprite; var b3:phx.Body; public function new(root){ this.root = root; var stage = root.stage; // define the size of the world var size = new phx.col.AABB(-1000,-1000,1000,1000); // create the broadphase : this is the algorithm used to optimize collision detection var bf = new phx.col.SortedList(); // initialize the world world = new phx.World(size,bf); // create one 20x20 box body at x=100,y=270 b3 = new phx.Body(100,270); b3.addShape( phx.Shape.makeBox(120,120) ); b3.mass=40; // add the created bodies to the world world.addBody(b3);
// creates a 380x50 box at x=0,y=280 var floor = phx.Shape.makeBox(380,50,0,280); // the floor is static, it can't move world.addStaticShape(floor);
// setup gravity world.gravity = new phx.Vector(0,0.9); mySprite= new Sprite(); mySprite.graphics.beginFill(0xaaaaff); mySprite.graphics.drawRect(0,0,15,15);
stage.addChild(mySprite); // for every frame, call the loop method stage.addEventListener(flash.events.Event.ENTER_FRAME,loop); stage.addEventListener(flash.events.MouseEvent.MOUSE_MOVE,myFunc); } public function myFunc(e : flash.events.MouseEvent) { mySprite.x += (root.mouseX - mySprite.x) / 2; mySprite.y += (root.mouseY - mySprite.y) / 2; } function loop(_) { // update the world world.step(0.8,20); mySprite.x += (b3.x - mySprite.x) / 3; mySprite.y += (b3.y - mySprite.y) / 3; //var mySpritePos = new phx.Vector(mySprite.x,mySprite.y); b3.y = mySprite.y; b3.x = mySprite.x; // clear the graphics var g = flash.Lib.current.graphics; g.clear(); // draw the world var fd = new phx.FlashDraw(g); fd.drawCircleRotation = true; fd.drawWorld(world); } static function main() { flash.Lib.current.stage.scaleMode = flash.display.StageScaleMode.NO_SCALE; inst = new Test2(flash.Lib.current); trace("click to fire"); } } On Wednesday 22 October 2008 09:53:21 Hugh Sanderson wrote: > Hi, > I think the basic idea would be the create the sprites, and each > sprite would "own" a "Body". So each sprite adds its body > to the world, and you add each sprite the the stage. > > Then, after you have updated the physics in "enter frame", > you would loop through each of your bodies and update > the graphics position/orientation from their "body" data. > > You would not call "FlashDraw". > > The only complication is setting up the callback to remove > the Sprite when the body is removed. > > Hugh -- haXe - an open source web programming language http://haxe.org |
Free forum by Nabble | Edit this page |