Box2D Forums

It is currently Mon May 20, 2013 11:31 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Feb 17, 2011 1:49 am 
Offline

Joined: Thu Feb 17, 2011 1:10 am
Posts: 9
I'm really new to WCK and the whole flash thing (1 day) so I might have overlooked something.

I tried reproducing the tutorial here
http://active.tutsplus.com/tutorials/ga ... r-an-hour/

When I create the HeroCharacter class in step 11, I get a runtime error (it doesn't stop the simulation) at the line
Code:
listenWhileVisible(world, StepEvent.STEP, world_stepEventHandler, false, 0, true);
The error message is
Code:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
   at misc::Entity/listenWhileVisible()
   at view.characters::HeroCharacter/create()
   at misc::Entity/ensureCreated()
   at Function/<anonymous>()
I've defined world_stepEventHandler as in the tutorial
Code:
private function world_stepEventHandler(e:StepEvent):void {}

Do you know what could be wrong ?
Or how I would go about debugging that ?


Last edited by vlad2048 on Thu Feb 17, 2011 12:29 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 5:58 am 
Offline

Joined: Sun Jan 30, 2011 7:46 am
Posts: 10
This error means that you tried to access to something that is not created yet.
There is some mistakes in this tutorial:
If you reproduce everything so you have this line in create function:
Code:
 listenWhileVisible(this, ContactEvent.BEGIN_CONTACT, this_beginContactHandler, false, 0, true);

But you didn't create this function yet (didn't write it's code), so write this after world_stepEventHandler Function:
Code:
private function this_beginContactHandler(e:ContactEvent):void
{
 
}


Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 9:30 am 
Offline

Joined: Thu Feb 17, 2011 1:10 am
Posts: 9
Thanks but no, I was careful to define that callback and the error happens the line before when I define the first callback.

Here's my whole code:
Code:
package view.characters
{
   import shapes.*;
   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 misc.*;
   import flash.utils.*;
   import flash.events.*;
   import flash.display.*;
   import flash.text.*;
   import flash.geom.*;
   import flash.ui.*;
   
   public class HeroCharacter extends Box
   {
      
      private var contacts:ContactList;
 
      public override function create():void {
         reportBeginContact = true;
         reportEndContact = true;
         contacts = new ContactList();
         contacts.listenTo(this);
   
         fixedRotation = true;
   
         listenWhileVisible(world, StepEvent.STEP, world_stepEventHandler, false, 0, true);
         listenWhileVisible(this, ContactEvent.BEGIN_CONTACT, this_beginContactHandler, false, 0, true);
   
         super.create();
      }
      
      private function world_stepEventHandler(e:StepEvent):void
      {
         var left:Boolean = Input.kd('LEFT', 'A');
         var right:Boolean = Input.kd('RIGHT', 'D');
         var jump:Boolean = Input.kp('UP', ' ', 'W');
      
         if (jump) {
            b2body.ApplyImpulse(new V2(0, -2), b2body.GetWorldCenter());
         }
         else if(left) {
            b2body.ApplyImpulse(new V2(-2, 0), b2body.GetWorldCenter());
         }
         else if(right) {
            b2body.ApplyImpulse(new V2(2, 0), b2body.GetWorldCenter());
         }
      }
      
      private function this_beginContactHandler(e:ContactEvent):void
      {
      
      }
      
   }

}
Notice the tutorial didn't say anything about the import directives, I copied them from World.as

Also I find it strange that the simulation doesn't stop ? The dummy box falls to the ground but I can't move the HeroCharacter box (because I suppose it's not initialized properly)

Any other ideas ?
Thanks!


Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 9:39 am 
Offline

Joined: Fri Dec 14, 2007 8:07 pm
Posts: 913
First problem I see at a glance: rearrange your "create()" function so super.create() is before the "listenWhileVisible" calls. Otherwise "world" isn't defined yet!


Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 10:09 am 
Offline

Joined: Thu Feb 17, 2011 1:10 am
Posts: 9
Thanks this was the error, it works now !
Indeed the tutorial was wrong, I've posted it in its comment section.

I suppose I could have found it myself if I could debug the code properly.

Can I put breakpoints, run step by step and watch variables at run time like in C++ ? Or how can I add messages in the Output pane ? Or what's the best way to debug ?


Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 10:21 am 
Offline

Joined: Sun Jan 30, 2011 7:46 am
Posts: 10
To add massage to output write:
Code:
trace("Your message");
trace([your var 1],[your var 2],....,[your var N]);


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 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