Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by coreysnyder

Below is my snowball class. I added one of these to my level via Weltmeister. I then later converted it over to a Box2D entity as seen below. After refreshing Weltmeister I get the error: Uncaught TypeError: Cannot read property 'CreateBody' of undefined on line 27. Does weltmeister not support the addition of Box2D entities?



ig.module(
    'game.entities.snowball'
).requires(
    'plugins.box2d.debug',
    'plugins.box2d.game',
    'plugins.box2d.entity'
).defines (function(){
    EntitySnowball = ig.Box2DEntity.extend({
        animSheet: new ig.AnimationSheet( 'media/web/ammo/fireball.png', 18, 18),
        size: {x: 18, y: 18},
        type: ig.Entity.TYPE.NONE,
        checkAgainst: ig.Entity.TYPE.B,
        collides: ig.Entity.COLLIDES.NEVER,
        bounciness: 0.6,
        init: function (x, y, settings){
            this.parent(x, y, settings);
            this.addAnim( 'idle', 1, [0]);
            //this.body.ApplyImpulse( new b2.Vec2(settings.velocity.x, settings.velocity.y), this.body.GetPosition() );
            var velocity = 50;

            var bodyDef = new b2.BodyDef();
            bodyDef.position.Set (
                x,
                y
            );
            console.log(ig.world);
            console.log(ig.world.CreateBody);
            var body = ig.world.CreateBody( bodyDef );

            var shapeDef = new b2.CircleDef();
            shapeDef.radius = (this.size.x/2) * b2.SCALE;

            body.CreateShape(shapeDef);
            body.SetMassFromShapes();
            this.body.ApplyImpulse(new b2.Vec2(velocity, -30), this.body.GetPosition());
        }
    });
});

1 decade ago by alexandre

Try this:

ig.module(
	'game.entities.snowball'
).requires(
	'plugins.box2d.debug',
	'plugins.box2d.game',
	'plugins.box2d.entity'
).defines (function(){

	EntitySnowball = ig.Box2DEntity.extend({

		animSheet: new ig.AnimationSheet( 'media/web/ammo/fireball.png', 18, 18),
		size: {x: 18, y: 18},
		type: ig.Entity.TYPE.NONE,
		checkAgainst: ig.Entity.TYPE.B,
		collides: ig.Entity.COLLIDES.NEVER,
		bounciness: 0.6,

		init: function (x, y, settings)
		{
			this.parent(x, y, settings);
			this.addAnim( 'idle', 1, [0]);
		},
		
		ready: function()
		{
			//this.body.ApplyImpulse( new b2.Vec2(settings.velocity.x, settings.velocity.y), this.body.GetPosition() );
			var velocity = 50;
			var bodyDef = new b2.BodyDef();
			bodyDef.position.Set (
				x,
				y
			);
			console.log(ig.world);
			console.log(ig.world.CreateBody);
			this.body = ig.world.CreateBody( bodyDef );
			var shapeDef = new b2.CircleDef();
			shapeDef.radius = (this.size.x/2) * b2.SCALE;
			this.body.CreateShape(shapeDef);
			this.body.SetMassFromShapes();
			this.body.ApplyImpulse(new b2.Vec2(velocity, -30), this.body.GetPosition());
		}
	});
});

You should avoid calling box2d functions from inside WM. You can check for that (whether or not you are inside wm) by checking the value of ig.global.wm.

Still, best is if you use the .ready function to do your setup. ready is called once the level has been loaded and all entities spawned. Calling box2d in init is asking for trouble because there is no guarantee of load order.

BTW, you had an error here, fixed by assigning to this.body instead:
var body = ig.world.CreateBody( bodyDef );

1 decade ago by coreysnyder

Thanks for the help Alexandre. If anyone copies this code above, be sure to add in the x,y params to the draw function like so:

ready: function(x,y){
      ......
}

1 decade ago by coreysnyder

BTW that was exactly the type of solution I was looking for.

1 decade ago by coreysnyder

Also, I stopped getting errors after this but the fire/snowballs stopped spawning. So I followed an example Dominic provided for circles so I modified my code to look like:

ready: function()
        {
            console.log("READY FUNCTION");
            this.createBody();
        },

        createBody: function(){
            console.log("CreateBody");
            var bodyDef = new b2.BodyDef();
            bodyDef.position.Set(
                (this.pos.x + this.size.x / 2) * b2.SCALE,
                (this.pos.y + this.size.y / 2) * b2.SCALE
            );

            this.body = ig.world.CreateBody(bodyDef);

            // These two lines define the shape
            // e.g. b2.PolygonDef, b2.CircleDef
            var shapeDef = new b2.CircleDef();
            shapeDef.radius = 8 * b2.SCALE;

            shapeDef.density = 1;
            this.body.CreateShape(shapeDef);
            this.body.SetMassFromShapes();
        }
Page 1 of 1
« first « previous next › last »