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 tunglxx226

I know that this problem was stated before and sorted out, but the solution just does not work for me :)

So here it goes my source code (actually I've copied it from the book Introducing HTML5 development);


ig.module
    (
        'game.entities.player'
    )

.requires
    (
        'impact.entity'
    )
.defines(function()
    {
        EntityPlayer = ig.Entity.extend
            ({
                animSheet: new ig.AnimationSheet('media/player.png', 16, 16),
                size: {x: 8, y: 14},
                offset: {x: 4, y: 2},
                flip: false,
                maxVel: {x: 100, y: 150},
                friction: {x: 600, y: 0},
                accelGround: 400,
                accelAir: 200,
                jump: 200,
                weapon: 0,
                activeWeapon: "EntityBullet",
                totalWeapons: 2,
                type: ig.Entity.TYPE.A,
                checkAgainst: ig.Entity.TYPE.NONE,
                collides: ig.Entity.COLLIDES.PASSIVE,
                init:function(x, y, settings)
                {
                    this.parent(x, y, settings)
                    this.addAnim('idle', 1, [0]);
                    this.addAnim('run', 0.07, [0, 1, 2, 3, 4, 5]);
                    this.addAnim('jump', 1, [9]);
                    this.addAnim('fall', 0.4, [6, 7]);
                },
                update:function()
                {
                  //move left or right:
                    var accel = this.standing? this.accelGround : this.accelAir;
                    if (ig.input.state('left'))
                    {
                        this.accel.x = -accel;
                        this.flip = true;
                    }
                    else if (ig.input.state('right'))
                    {
                        this.accel.x = accel;
                        this.flip = false;
                    }
                    else
                    {
                        this.accel.x = 0;
                    }
                  // jump:
                   if (this.standing && ig.input.pressed('jump'))
                   {

                       this.vel.y = -this.jump;
                   }
                    // switch:
                    if( ig.input.pressed('switch') ) {
                        this.weapon ++;
                        if(this.weapon >= this.totalWeapons)
                            this.weapon = 0;
                        switch(this.weapon){
                            case(0):
                                this.activeWeapon = "EntityBullet";
                                break;
                            case(1):
                                this.activeWeapon = "EntityGrenade";
                                break;
                        }
                    }
                   // shoot:
                    if( ig.input.pressed('shoot') ) {
                        ig.game.spawnEntity( this.activeWeapon, this.pos.x, this.pos.y, {flip:this.flip} );
                    }

                    // set the current animation based on the player's speed:
                    if (this.vel.y < 0)
                    {
                        this.currentAnim = this.anims.jump;
                    }
                    else if (this.vel.y > 0)
                    {
                        this.currentAnim = this.anims.fall;
                    }
                    if (this.accel.x != 0)
                    {
                        this.currentAnim = this.anims.run;
                    }
                    else
                    {
                        this.currentAnim = this.anims.idle;
                    }
                  //move:
                    this.currentAnim.flip.x = this.flip;
                    this.parent();

                }

            });
        EntityBullet = ig.Entity.extend(
            {
                size: {x:5, y:3},
                animSheet: new ig.AnimationSheet('media/bullet.png', 5, 3),
                maxVel: {x:100, y:0},
                type: ig.Entity.TYPE.NONE,
                checkAgainst: ig.Entity.TYPE.B,
                collides: ig.Entity.COLLIDES.PASSIVE,
                flip: false,
                init:function(x, y, settings)
                {
                    this.parent(x + (settings.flip ? -4 : 8) , y+8, settings); // to set the position of the bullet in relation with the player entity
                    this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x); // to set the direction and speed of the bullet in relation with the player entity
                    this.flip = settings.flip;
                    this.addAnim('idle', 0.2, [0]);
                },
                handleMovementTrace: function(res)
                {
                    this.parent(res);
                    if (res.collision.x || res.collision.y)
                    {
                        this.kill();
                    }
                },
                check:function( other )
                {
                    other.receiveDamage(3, this);
                    this.kill();
                }
            });
        EntityGrenade = new ig.Entity.extend(
            {
                size: {x:4, y:4},
                offset: {x:2, y:2},
                animSheet: new ig.AnimationSheet('media/grenade.png', 8, 8),
                type: ig.Entity.TYPE.NONE,
                checkAgainst: ig.Entity.TYPE.BOTH,
                collides: ig.Entity.COLLIDES.PASSIVE,
                maxVel: {x:200, y:200},
                bounciness: 0.6,
                bounceCounter:0,

                init: function( x, y, settings ) {
                    this.addAnim( 'idle', 0.2, [0,1] );
                    this.parent( x + (settings.flip ? -4 : 7), y, settings );
                    this.vel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
                    this.vel.y = -(50 + (Math.random()*100));

                },
                handleMovementTrace: function( res ) {
                    this.parent( res );
                    if( res.collision.x || res.collision.y ) {
                    // only bounce 3 times
                        this.bounceCounter++;
                        if( this.bounceCounter > 3 ) {
                            this.kill();
                        }
                    }
                },
                check: function( other ) {
                    other.receiveDamage( 10, this );
                    this.kill();
                }
            });
    });

and here is the error I got:

Uncaught TypeError: object is not a function impact.js:435
288Uncaught Can't spawn entity of type EntityGrenade /residentraver/lib/impact/game.js:129
370
Uncaught Can't spawn entity of type EntityGrenade

Any idea what happened? Thanks in advanced.

1 decade ago by quidmonkey

This line:
EntityGrenade = new ig.Entity.extend(

Should be:
EntityGrenade = ig.Entity.extend(

1 decade ago by tunglxx226

oh right, I didn't notice that, thank you very much
Page 1 of 1
« first « previous next › last »