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 mk1sg633

i write some code and spawn the Entity in init ,

but it have a error in weltmeister TypeError: ig.game.spawnEntity is not a function

why i can't spawn the Entity in init?

here is my code

ig.module(
	'game.entities.shipb'
)
.requires(
	'impact.entity',
	'impact.game'
)
.defines(function(){

EntityShipb = ig.Entity.extend({
	size: {x: 137, y: 77},
	maxVel: {x: 100, y: 0},
	friction: {x: 150, y: 0},
	
	type: ig.Entity.TYPE.B, // Evil enemy group
	checkAgainst: ig.Entity.TYPE.A, // Check against friendly
	collides: ig.Entity.COLLIDES.NONE,
	
	health: 150,
	speed: 14,
	flip: false,
	player: null,
	armOffsets:[
                {x:0,y:10,timerOffset:3},
            ],
        children:[],
        oscillationTimer: new ig.Timer(),
	
	animSheet: new ig.AnimationSheet( 'media/big/ship_1.png', 274, 154 ),
	
	
	init: function( x, y, settings ) {
		this.parent( x, y, settings );
                this.vel.x = 10;
		this.addAnim( 'crawl', 1, [0] );
		ig.game.spawnEntity(EntityEnemyGun,this.pos.x-32,this.pos.y-11);
		
	},
        
	draw: function(){
		var ctx = ig.system.context;
		ctx.save();
		ctx.translate( ig.system.getDrawPos( this.pos.x - this.offset.x - ig.game.screen.x ),
		ig.system.getDrawPos( this.pos.y - this.offset.y - ig.game.screen.y ) );
		ctx.scale( 0.5, 0.5 );
		this.currentAnim.draw( 0, 0 );
		ctx.restore();
	},
	
	
	update: function() {
		// near an edge? return!
		this.parent();
		if( !ig.game.collisionMap.getTile(
			this.pos.x + (this.flip ? +4 : this.size.x -4),
			this.pos.y + this.size.y))
		{
			this.flip = !this.flip;
		}
		
		var xdir = this.flip ? -1 : -1;
		this.vel.x = 14 * xdir;
		this.pos.y = 468;
		this.updateChildren();
		if (this.children=="") {
		this.attachGuns(0);
		
		}
		
	},

	    
	attachGuns: function(offset){
		var f = ig.game.spawnEntity(EntityEnemyGun,this.pos.x-32,this.pos.y-11);
		this.children.push(f);
                var c = ig.game.spawnEntity(Entityeney,this.pos.x,this.pos.y+10);
		this.children.push(c);
            },

	    
	updateChildren: function () {
		
		if (!this.children.length) return;	
		var cover = this.children[0];
		cover.pos.x =this.pos.x-32;
		cover.pos.y =this.pos.y-10;
		cover.zindex = 10;
		ig.game.sortEntitiesDeferred();
		var canon = this.children[1];
		canon.pos.x =this.pos.x;
		canon.pos.y =this.pos.y+10;
		canon.zindex = cover.zindex-10;
		this.parent();
		ig.game.sortEntitiesDeferred();
            },
	
	
	handleMovementTrace: function( res ) {
		this.parent( res );
		
		// collision with a wall? return!
		if( res.collision.x ) {
			this.flip = !this.flip;
		}
	},	
	collideWith : function( other, axis ) {
		
		
		this.parent( other , axis );
		
	},
	check: function( other  ) {
		
		this.receiveDamage(100);
		this.children[0].receiveDamage(100);
		this.children[1].receiveDamage(100);
	}
});

Entityeney = ig.Entity.extend({
	size: {x: 54, y: 40},
	offset: {x: 2, y: 2},
	maxVel: {x: 0, y: 0},
	health:100,
	
	
	// The fraction of force with which this entity bounces back in collisions

	
	type: ig.Entity.TYPE.NONE,
	checkAgainst: ig.Entity.TYPE.B, // Check Against B - our evil enemy group
	collides: ig.Entity.COLLIDES.NONE,
		
	animSheet: new ig.AnimationSheet( 'media/big/ship_1_canon_cover.png', 54, 40 ),
	
	
	init: function( x, y, settings ) {
		this.parent( x, y, settings );
		this.addAnim( 'idle', 1, [0] );
	},
		
	handleMovementTrace: function( res ) {
		this.parent( res );

	},
	
	// This function is called when this entity overlaps anonther entity of the
	// checkAgainst group. I.e. for this entity, all entities in the B group.
	check: function( other ) {
		
	}	
});

EntityEnemyGun   = Entityeney.extend({
	
	health:100,
	bullets:1,
        firingTimer: null,
        reloadTime: 3,

		
	animSheet: new ig.AnimationSheet('media/big/ship_1_canon.png',54,44),
	
	
        init: function(x,y,settings){
		this.parent(x,y,settings);
		this.addAnim('idle', 0.2, [0,1,2,3,4,5]);
		this.firingTimer = new ig.Timer(this.reloadTime );
        },
		
        update: function() {
                this.parent();
                if (this.firingTimer.delta() > 0) {
			
                    var angleIncrease = 360 / (this.bullets - 1);
                    var initAngle = 200;
                    for (var i = 0; i < this.bullets; i++) {
                        var angle = initAngle * Math.PI / 180;
                        var x = this.pos.x ;
                        var y = this.pos.y ;
                        ig.game.spawnEntity(EntityEnemyBullet, x+4, y+20, {
                            angle: angle
			    
                        });
			
			this.currentAnim = this.anims.idle;
			
                    }
                    this.firingTimer.set(this.reloadTime);
                }
        		
		
	
	}
	
	
});

EntityEnemyBullet = ig.Entity.extend({
	size: {x: 25, y: 25},
	offset: {x: 10, y: 0},
	maxVel: {x: 300, y: 300},
            animSheet: new ig.AnimationSheet('media/big/canon_ball.png',25,25),
            health: 10,
            bounciness: 0.6, 
	
	type: ig.Entity.TYPE.B,
	checkAgainst: ig.Entity.TYPE.A, // Check Against B - our evil enemy group
	collides: ig.Entity.COLLIDES.NONE,
	    bounceCounter: 0,
	    
	    
	init: function(x,y,settings){
                this.parent(x,y,settings);
                this.addAnim('idle', 1, [0]);
                this.vel.x = -200
		this.vel.y = -200;
        },
	update: function () {
                this.parent();

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

});




});

1 decade ago by lazer

I'm pretty sure Weltmeister can't access that function by default. There was a thread about this a while back: http://impactjs.com/forums/help/spawn-entity-on-init-weltmeister-error/page/1

1 decade ago by mk1sg633

thank you lazer

but i still don't know how to fix this problem ...

1 decade ago by drhayes

I'd try three things:

1. You are using the entity EntityEnemyGun but I don&039;t see a matching file in your #require statement. You need to tell Impact where to find the entity file. Probably 'game.entities.enemyGun'?

2. If you need to spawn the enemy gun when this guy spawns, stick the ig.game.spawnEntity in the ready function instead.

3. Do what the thread Lazer pointed you to says to do and put the spawn code in this if:

if (!ig.global.wm) { ... 
}
Page 1 of 1
« first « previous next › last »