Box2D Forums

It is currently Sun May 19, 2013 12:11 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 15 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Mon Feb 21, 2011 12:00 pm 
Offline

Joined: Mon Feb 21, 2011 10:57 am
Posts: 14
I was wondering if any body could explain to me how I can make this code from a tutorial I found. Work with the code in the WCK files.
Here is the link to the tutorial.
http://www.emanueleferonato.com/2009/09 ... prototype/
This is the code from the tutorial that I want to work with WCK.
I have done alot of tutorials for BOX2D and understand alot about it. But I find its just easier to make my game in WCK. If any buddy can help me or point me in the right direction as to how to get this code to work with WCK I would greatly appreciate it immensely. SO please help me I am totally lost right now. Thanks in advance.
Code:
package {
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;
   public class HelloWorld extends Sprite {
      public var m_world:b2World;
      public var m_iterations:int=10;
      public var m_timeStep:Number=1.0/30.0;
      public var the_arrow:arrow=new arrow();
      public var the_arrow_angle:Number;
      public var my_canvas:Sprite = new Sprite();
      public var deg_to_rad:Number=0.0174532925;
      public var rad_to_deg:Number=57.2957795;
      public var charging:Boolean=false;
      public var power:int=0;
      public var impulse:Boolean=false;
      public function HelloWorld() {
         addChild(the_arrow);
         addChild(my_canvas);
         var worldAABB:b2AABB = new b2AABB();
         worldAABB.lowerBound.Set(-100.0, -100.0);
         worldAABB.upperBound.Set(100.0, 100.0);
         var gravity:b2Vec2=new b2Vec2(0.0,10.0);
         var doSleep:Boolean=true;
         m_world=new b2World(worldAABB,gravity,doSleep);
         // debug draw
         var m_sprite:Sprite;
         m_sprite = new Sprite();
         addChild(m_sprite);
         var dbgDraw:b2DebugDraw = new b2DebugDraw();
         var dbgSprite:Sprite = new Sprite();
         m_sprite.addChild(dbgSprite);
         dbgDraw.m_sprite=m_sprite;
         dbgDraw.m_drawScale=30;
         dbgDraw.m_alpha=1;
         dbgDraw.m_fillAlpha=0.5;
         dbgDraw.m_lineThickness=1;
         dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;
         m_world.SetDebugDraw(dbgDraw);
         // end debug draw
         // Vars used to create bodies
         var body:b2Body;
         var bodyDef:b2BodyDef;
         var boxDef:b2PolygonDef;
         var circleDef:b2CircleDef;
         // Add ground body
         bodyDef = new b2BodyDef();
         bodyDef.position.Set(10, 14);
         boxDef = new b2PolygonDef();
         boxDef.SetAsBox(30, 1);
         boxDef.friction=0.3;
         boxDef.density=0;
         body=m_world.CreateBody(bodyDef);
         body.CreateShape(boxDef);
         body.SetMassFromShapes();
         //
         bodyDef = new b2BodyDef();
         bodyDef.position.Set(5, 5);
         circleDef = new b2CircleDef();
         circleDef.radius=0.5;
         circleDef.density=1.0;
         circleDef.friction=0.5;
         circleDef.restitution=0.2;
         bodyDef.userData = new Sprite();
         bodyDef.userData.name="Player";
         body=m_world.CreateBody(bodyDef);
         body.CreateShape(circleDef);
         body.SetMassFromShapes();
         //
         addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
         stage.addEventListener(MouseEvent.MOUSE_UP, shoot);
         stage.addEventListener(MouseEvent.MOUSE_DOWN,charge);
 
      }
      public function Update(e:Event):void {
         m_world.Step(m_timeStep, m_iterations);
         for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
            if (bb.GetUserData()!=null) {
               the_arrow.x=bb.m_userData.x=bb.GetPosition().x*30;
               the_arrow.y=bb.m_userData.y=bb.GetPosition().y*30;
               var dist_x=the_arrow.x-mouseX;
               var dist_y=the_arrow.y-mouseY;
               the_arrow_angle=Math.atan2(- dist_y,- dist_x);
               the_arrow.rotation=the_arrow_angle*rad_to_deg;
               if (charging) {
                  power++;
                  if (power>=120) {
                     power-=120;
                  }
                  my_canvas.graphics.clear();
                  my_canvas.graphics.lineStyle(3,0x000000,0.5);
                  draw_arc(my_canvas,the_arrow.x=bb.m_userData.x=bb.GetPosition().x*30,the_arrow.y=bb.m_userData.y=bb.GetPosition().y*30,20,270,270+power*3,1);
               }
               if (impulse) {
                  bb.ApplyImpulse(new b2Vec2(Math.cos(the_arrow_angle)*power/4, Math.sin(the_arrow_angle)*power/4),bb.GetWorldCenter());
                  impulse=false;
                  power=0;
               }
            }
         }
      }
      public function charge(e:MouseEvent):void {
         charging=true;
      }
      public function shoot(e:MouseEvent):void {
         charging=false;
         my_canvas.graphics.clear();
         impulse=true;
      }
      public function draw_arc(movieclip,center_x,center_y,radius,angle_from,angle_to,precision):void {
         var angle_diff=angle_to-angle_from;
         var steps=Math.round(angle_diff*precision);
         var angle=angle_from;
         var px=center_x+radius*Math.cos(angle*deg_to_rad);
         var py=center_y+radius*Math.sin(angle*deg_to_rad);
         movieclip.graphics.moveTo(px,py);
         for (var i:int=1; i<=steps; i++) {
            angle=angle_from+angle_diff/steps*i;
            movieclip.graphics.lineTo(center_x+radius*Math.cos(angle*deg_to_rad),center_y+radius*Math.sin(angle*deg_to_rad));
         }
      }
 
 
   }
}


Top
 Profile  
 
PostPosted: Tue Feb 22, 2011 9:59 am 
Offline

Joined: Tue Nov 09, 2010 1:26 am
Posts: 92
What part of it in particular would you like to tackle first? I'm willing to spend some time over the next few days (have to work and sleep as well, ;) ) helping you with an example like this.

I have to point out that while Emanuele Feronato does some cool stuff on his blog, his code is really messy and is incredibly hard to follow without the site because he doesn't comment his code, he mostly does the commenting there on his site. So maybe in the process we can adapt his code, make a tutorial of it here on the forums, and people can actually follow along with it. I tend to approach programming very simply so when I don't see any attempt to speak to me in plain english, I tend to get pretty confused pretty quickly. On that note, thanks mayo for commenting your code!

So yea, I don't think this code is that complex, it's just messy and I'll try and post something tonight (12pm here right now)

Later


Top
 Profile  
 
PostPosted: Tue Feb 22, 2011 10:21 am 
Offline

Joined: Mon Feb 21, 2011 10:57 am
Posts: 14
OMG thank you soo much. Basically all i want is the controls for it. I want it to follow the mouse and when I hold it down it charges and shoots in the direction that the mouse is pointing. Also I like the charge that it makes around the ball indicating that its charging.

I am working on it right now just trying to mix parts of the code in with already created .as files like the boxman.as file I copied and I am trying to remove the keyboard controls and replacing it with the mouse controls from emanuel's pumpkin tutorial. I am basically just doing trial and error with very little success.

Box2d and WCK are both new to me so I am just diving into the code try to understand how they both connect and work. Its very challenging but I am more then willing to figure it out.

Ya I found his tutorials very helpful but I totally agree there is no explanation for me to fully grasp what he is teaching.

The code does exactly what I want my game to do. Its just outdated and no explanation that makes it hard.
Thanks again for your help I am extremely grateful any help.


Top
 Profile  
 
PostPosted: Tue Mar 01, 2011 10:08 am 
Offline

Joined: Mon Feb 21, 2011 10:57 am
Posts: 14
So I am still having issues with getting this code to work with WCK. I am going to go into as much detail with this issue as I can because I desperately need help. So I took parts of the code that I used from above and pasted them into my own .as file called Golf.as.
I basically combined the boxman.as file and the tutorial file above. then removed the Boxman keyboard controls and replaced them with the mouse events from the tutorial.

I did "public class Golf extends Circle{" because I want my hero to function like a ball.


So here is what I do.
I have Golf.as saved in the demo folder.
In the Library I right click my "ball" movie clip go to properties and set the Class to demo.Golf. (For some reason I can't just type Golf in the base class. I need to have it be demo.Golf so it can find it. If you look at Boxman in the demo flash file it just has BoxMan and it finds the .as file in the demo folder. So that might be part of the problem but i can't figure out why.)

Then I go to Component Definitions and set the class to wck.BodyShape.
When I publish it the ball just sits there and doesn't move at all.

Also I get this error

C:\Users\Chad\Documents\Design Documents\Game\jesses-wck-2b31de3\demo\Golf.as, Line 1 5001: The name of package '' does not reflect the location of this file. Please change the package definition's name inside this file, or move the file. C:\Users\Chad\Documents\Design Documents\Game\jesses-wck-2b31de3\demo\Golf.as

Here is the code I modified with the tutorial and the boxman.as file. I am totally lost and need some help. please if there is any Box2D guru's out there that could take a look at this I would be forever grateful.

Here is the code I modified with the tutorial and the boxman.as file.

Code:
package {
   
   import Box2D.Dynamics.b2World;
   import Box2DAS.*;
   import Box2DAS.Collision.*;
   import Box2DAS.Collision.Shapes.*;
   import Box2DAS.Common.*;
   import Box2DAS.Dynamics.*;
   import Box2DAS.Dynamics.Contacts.*;
   import Box2DAS.Dynamics.Joints.*;
   import cmodule.Box2D.*;
   import wck.*;
   import shapes.*;
   import misc.*;
   import extras.*;
   import flash.utils.*;
   import flash.events.*;
   import flash.display.*;
   import flash.text.*;
   import flash.geom.*;
   
   public class Golf extends Circle{
      public var m_world:b2World;
      public var m_iterations:int=10;
      public var m_timeStep:Number=1.0/30.0;
      public var the_arrow:Sprite = new Sprite();
      public var the_arrow_angle:Number;
      public var my_canvas:Sprite = new Sprite();
      public var deg_to_rad:Number=0.0174532925;
      public var rad_to_deg:Number=57.2957795;
      public var charging:Boolean=false;
      public var power:int=0;
      public var impulse:Boolean=false;
      public var contacts:ContactList;
      
      public override function create():void {
         reportBeginContact = true;
         reportEndContact = true;
         fixedRotation = true;
         super.create();
         listenWhileVisible(world, StepEvent.STEP, parseInput, false, 10);
         listenWhileVisible(this, ContactEvent.BEGIN_CONTACT, handleContact);
         contacts = new ContactList();
         contacts.listenTo(this);
      }
      
      public function handleContact(e:ContactEvent):void {
         var p:Pellet = e.other.m_userData as Pellet;
         if(p) {
            Util.addChildAtPosOf(world, new FX1(), p); 
            p.remove();
         }
      }
      
      public function Update(e:Event):void {
         m_world.Step(m_timeStep, m_iterations);
         for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
            if (bb.GetUserData()!=null) {
               the_arrow.x=bb.m_userData.x=bb.GetPosition().x*30;
               the_arrow.y=bb.m_userData.y=bb.GetPosition().y*30;
               var dist_x=the_arrow.x-mouseX;
               var dist_y=the_arrow.y-mouseY;
               the_arrow_angle=Math.atan2(- dist_y,- dist_x);
               the_arrow.rotation=the_arrow_angle*rad_to_deg;
               if (charging) {
                  power++;
                  if (power>=120) {
                     power-=120;
                  }
                  my_canvas.graphics.clear();
                  my_canvas.graphics.lineStyle(3,0x000000,0.5);
                  draw_arc(my_canvas,the_arrow.x=bb.m_userData.x=bb.GetPosition().x*30,the_arrow.y=bb.m_userData.y=bb.GetPosition().y*30,20,270,270+power*3,1);
               }
               if (impulse) {
                  bb.ApplyImpulse(new b2Vec2(Math.cos(the_arrow_angle)*power/4, Math.sin(the_arrow_angle)*power/4),bb.GetWorldCenter());
                  impulse=false;
                  power=0;
               }
            }
         }
      }
      public function charge(e:MouseEvent):void {
         charging=true;
      }
      public function shoot(e:MouseEvent):void {
         charging=false;
         my_canvas.graphics.clear();
         impulse=true;
      }
      public function draw_arc(movieclip,center_x,center_y,radius,angle_from,angle_to,precision):void {
         var angle_diff=angle_to-angle_from;
         var steps=Math.round(angle_diff*precision);
         var angle=angle_from;
         var px=center_x+radius*Math.cos(angle*deg_to_rad);
         var py=center_y+radius*Math.sin(angle*deg_to_rad);
         movieclip.graphics.moveTo(px,py);
         for (var i:int=1; i<=steps; i++) {
            angle=angle_from+angle_diff/steps*i;
            movieclip.graphics.lineTo(center_x+radius*Math.cos(angle*deg_to_rad),center_y+radius*Math.sin(angle*deg_to_rad));
         }
      }
   }
}


Top
 Profile  
 
PostPosted: Tue Mar 01, 2011 10:20 am 
Offline

Joined: Tue Nov 09, 2010 1:26 am
Posts: 92
Dude, I'm sorry I forgot about helping you. I'll make some time in the next 8-10 hours to take a look at this and see if I can help. Sorry again for leaving you hanging. While I can't help at this moment, I still wanted to go ahead and acknowledge I left you hanging.

Talk soon. (It's now on my calendar for today as well)


Top
 Profile  
 
PostPosted: Tue Mar 01, 2011 10:28 am 
Offline

Joined: Tue Nov 09, 2010 1:26 am
Posts: 92
Are you using CS4 or CS5? Just wanted to make sure if I made a file and uploaded it you could use it.


Top
 Profile  
 
PostPosted: Tue Mar 01, 2011 10:58 am 
Offline

Joined: Mon Feb 21, 2011 10:57 am
Posts: 14
That's no problem at all. I understand people have lives and there own problems on this forum there trying to figure out. I just wish I could figure it out by myself.

I forgot to add some of the other errors I received hope it helps in some anyway. I am using Adobe CS5 publishing it to Flash player 10.

So here is all the errors. Thanks again for your time.

C:\Users\Chad\Documents\Design Documents\Game\jesses-wck-2b31de3\demo\Golf.as, Line 1 5001: The name of package '' does not reflect the location of this file. Please change the package definition's name inside this file, or move the file. C:\Users\Chad\Documents\Design Documents\Game\jesses-wck-2b31de3\demo\Golf.as

C:\Users\Chad\Documents\Design Documents\Game\jesses-wck-2b31de3\demo\Golf.as, Line 1 5000: The class 'demo.Golf' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

C:\Users\Chad\Documents\Design Documents\Game\jesses-wck-2b31de3\shapes\Box.as, Line 1 5000: The class 'shapes.Box' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

C:\Users\Chad\Documents\Design Documents\Game\jesses-wck-2b31de3\wck\WCK.as, Line 1 5000: The class 'wck.WCK' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.

C:\Users\Chad\Documents\Design Documents\Game\jesses-wck-2b31de3\wck\World.as, Line 1 5000: The class 'wck.World' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.


Top
 Profile  
 
PostPosted: Tue Mar 01, 2011 5:07 pm 
Offline

Joined: Tue Nov 09, 2010 1:26 am
Posts: 92
Yea, a lot of the issues that you'll encounter in terms of errors in the Flash IDE are going to be the result of something up the food chain breaking first. I believe the fact that the swc that gets generated at runtime and is used is the culprit for the assault of error messages you get. Usually you can look at the first couple and say, "yea that's the class file I was just working in" and know where you need to address your problems. The ones that say, "must subclass MovieClip" are a good example of the "something broke before it got here" type of error.

I've got the code working. I'm going to head home and prep for a call I've got. After that I'll comment everything up and publish the files here if I can attach them. Otherwise I'll post a dropbox link.

Later.


Top
 Profile  
 
PostPosted: Wed Mar 02, 2011 9:15 am 
Offline

Joined: Mon Feb 21, 2011 10:57 am
Posts: 14
Thanks you so much I look forward to seeing it. :D


Top
 Profile  
 
PostPosted: Wed Mar 02, 2011 11:14 am 
Offline

Joined: Tue Nov 09, 2010 1:26 am
Posts: 92
Dude I've been so busy. Was up until 4:30 this morning writing a project proposal... that said, here are the files. The commenting isn't where I want it to be. I did this in my "not so free" time yesterday. The files should fit nicely in the same project heirarchy as the demo files.

Take care. Ping me back with any questions.

WCK Golfball example files


Last edited by burtonposey on Mon Apr 16, 2012 4:10 pm, edited 1 time in total.

Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group