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 JackImpact

Hi all,

I am stuck with this weird bug for two days now. All the efforts result no success. I hope any expert can help. I believe the problem is to do with the Director plugin. But not sure.

Basically I am developing a shooting game. There is an arrow in the game which can shoot bullets. The bullet shooting works as expected when the game is loaded for the first time. But If I bring up the menus by clicking a menu button and chose another level to play, the arrow doesn’t spawn a bullet at the first mouse click but spawn bullets on the subsequence clicks.

Besides the menu button, I also have a reload button and next level button. All these two buttons are working fine. When reloaded or going to the next level, the arrow’s behavior is perfect. Only this Menu button is not working.

When the menu button is clicked, it changes the game’s state to menu state and show the world choice menu on which you can choose the world, then choose a level in the world. Once the level button is clicked, the level choice menu is hidden and the level is loaded. Then the problem comes in. First click, no bullet.

Please note, I am using the Director Plugin by Boneheadmed (https://github.com/boneheadmed/Director) to manage the levels. The world choice menu and level choice menu is created using jquery and DOM elements.

Here are the relevant code snippets:
button click to hide the menu, change the game state and reset all the game variables

$("#btnPark1").click(

				function(){

				            $("#menuLevelsPark").hide();
							
							ig.game.gameStateInParkLevelMenu=false;
							




							ig.game.myDirector.loadLevel(0);
							ig.game.currentLevelVars = clone( ig.game.levelVars[ 0 ] );
							
							ig.game.generateBtnAndMenu();
							
							
							ig.game.lives=ig.game.livesB+ig.game.currentLevelVars.lives;
							ig.game.powerUpNum=ig.game.powerUpNumB+ig.game.currentLevelVars.powerUpNum;
							ig.game.iceCreamNum=ig.game.iceCreamNumB+ig.game.currentLevelVars.iceCreamNum;
							ig.game.archNum=ig.game.archNumB+ig.game.currentLevelVars.archNum;

							

							ig.game.gameTimer.reset();
					     }

			);

Arrow to shoot bullet

EntityArrow=ig.Entity.extend({
		
		   animSheet: new ig.AnimationSheet( 'media/arrowSpace.png', 50, 100 ),
		   size: {x: 50, y:100},
		   offset: {x: 0, y: 0},
		   flip: false,
		   zIndex:300,
		   aPaused:false,
		   name:'arrow',
		   //weapon:'splat',
		   weapon:'bullet',
		   alphaFactor:0,
		   collides: ig.Entity.COLLIDES.NEVER,
		   //checkAgainst: ig.Entity.TYPE.B,
		   
		   init: function( x, y, settings ) {
				this.parent( x, y, settings );
				this.addAnim( 'idle', 1, [0] );
				
				},
		   
		   update:function(){
			   
			  if(ig.input.pressed('shoot')){
						
						console.log('arrow paused:'+this.aPaused);
						}
			   
			   if(!this.aPaused){
					x = this.pos.x+this.size.x/2;
					y = this.pos.y+this.size.y;
					mx = ig.input.mouse.x;
					my = ig.input.mouse.y;
					
					this.alphaFactor+=Math.PI/180;
					if(this.alphaFactor>Math.PI){
						this.alphaFactor=0;
						}
					shootingAngle=Math.atan2((my-y),(mx-x))+Math.PI/2;
					
					this.anims.idle.pivot={x:25,y:100};
					this.anims.idle.angle=shootingAngle;
					this.anims.idle.alpha=0.2+Math.sin(this.alphaFactor);
					
					//console.log(this.anims.idle.alpha);
					 if(ig.input.pressed('shoot')){
						console.log("inner shoot");
						}
					
					
				
				   if( ig.input.pressed( 'shoot' )&&ig.game.getEntitiesByType(EntityBullet).length==0&&ig.game.getEntitiesByType(EntityBulletLittle).length==0){
				
					   
					  // console.log("shootPressed: "+ig.input.pressed('shoot'));
					 //  console.log("powerUpClickNum: "+ig.game.powerUpClickNum);
					  // console.log("BulletNum: "+ig.game.getEntitiesByType(EntityBullet).length);
					  // console.log("LivesNum: "+ig.game.lives);
					   
					  
					   if(this.weapon==='bullet'){
							
							if(ig.game.lives>0){
							//ig.game.spawnEntity(EntityBullet,330,355);
							ig.game.spawnEntity(EntityBullet,335,380);
							//ig.game.spawnEntity(EntityIceCream,this.pos.x,this.pos.y);
							//ig.game.spawnEntity(EntityBullet,this.pos.x,this.pos.y);
							}
		
						}
						
						else if(this.weapon==='powerUp'){
							if(ig.game.powerUpNum>0){
						      ig.game.spawnEntity(EntityPowerUpW,335,380);
							 }
							
						}
						
						else if(this.weapon==='splat'){
							if(ig.game.archNum>0){
							//alert('splat');
							ig.game.spawnEntity(EntityPowerUpM,335,380);	
							}
						}
						
						//else{
//							return;	
//						}
						
						
					
				   }
				   
				  
				   }//end of paused condition
				   		   
			 		this.parent();
			  },
		   
		});


Any hero can help to find this monster bug?

1 decade ago by quidmonkey

Is there any chance ig.game.lives isn&039;t properly set initially? Maybe it's a String vs. a Number (level vars often come in as Strings)? Do #this.weapon or this.aPaused ever change?

1 decade ago by JackImpact

Hi quidmonkey,

Nice to meet you here again! all the calculation and action based on the ig.game.lives are correct and it behave like an integer as expeccted. So I don't think it is because of ig.game.lives.

this.weapon dose change when the player clicks a correspondent button in a in-game menu or a bullet hit a power-up icon.

this.aPaused is a booleen, it is used to pause the arrow when the mouse hover on the in-game menu and buttons(the arrow changes its pointing direction based on the mouse position). I did bit of debugging before. Even I removed if(!this.aPaused){} condition, the bug is still there.

The bug only comes in when I bring up the game menus created by jquery and DOM elements and chose another level to play. It dosn't come in when I use the in-game buttons to reload the level and go to next level. It even dosn't come in when I first load up the whole game.

I suspect it is to do with the event dispatching. The game use left mouse click for shooting bullets, bring up menus and click buttons. Could that be the reason??

for your reference, I have attached the in-game reload button's code

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

.requires(
  'impact.entity',
  'game.entities.button'
)

.defines(function() {

EntityBtnReload = EntityButton.extend({

    size: { x: 40, y: 40 },

    text: [''],
    textPos: { x: 12, y: 5 },
    textAlign: ig.Font.ALIGN.LEFT,

    zIndex:4001,
    font: null,
	name:'btnReload',


    animSheet: new ig.AnimationSheet( 'media/btnReload.png', 40, 40 ),

	update:function(){

		this.parent();
		},

    pressedDown: function() {

		ig.game.myDirector.reloadLevel();
		//console.log('next level');

		//ig.game.currentLevelVars=ig.game.levelVars[ig.game.myDirector.currentLevel];
		ig.game.currentLevelVars = clone( ig.game.levelVars[ig.game.myDirector.currentLevel] );

		//regenerate all the buttons and menus
		ig.game.generateBtnAndMenu();

		ig.game.currentLevelVars.enemyCreated=0;
		ig.game.currentLevelVars.enemyKilled=0;

		//lives num was resumed when the level is reloaded? Promote the player to buy more points to reload?
		ig.game.lives=ig.game.livesB+ig.game.currentLevelVars.lives;
		ig.game.powerUpNum=ig.game.powerUpNumB+ig.game.currentLevelVars.powerUpNum;
		ig.game.iceCreamNum=ig.game.iceCreamNumB+ig.game.currentLevelVars.iceCreamNum;
		ig.game.archNum=ig.game.archNumB+ig.game.currentLevelVars.archNum;

		//reset game timer
		ig.game.gameTimer.reset();


		},

    pressed: function() {
		},
    pressedUp: function() {},

	hoverIn:function(){

	},

	hoverOut:function(){


	},

  });

});

hopefully these will give you a hint to spot the bug.

Big thanks in advance!

1 decade ago by quidmonkey

I can't tell from your code. Is one of your if statements failing on the way down?
update: function () {

	if (ig.input.pressed('shoot')) {
		console.log('arrow paused:' + this.aPaused);
	}

	// does this.aPaused = false?
	if (!this.aPaused) {
		// code

		// is shoot pressed and do such entities exist?
		if (ig.input.pressed('shoot') && ig.game.getEntitiesByType(EntityBullet).length == 0 && ig.game.getEntitiesByType(EntityBulletLittle).length == 0) {

			// is the proper weapon set?
			if (this.weapon === 'bullet') {

				// do we have lives?
				if (ig.game.lives > 0) {
					//ig.game.spawnEntity(EntityBullet,330,355);
					ig.game.spawnEntity(EntityBullet, 335, 380);
					//ig.game.spawnEntity(EntityIceCream,this.pos.x,this.pos.y);
					//ig.game.spawnEntity(EntityBullet,this.pos.x,this.pos.y);
				}

				//code
			}

		}

	} //end of paused condition

	this.parent();
}

I see four ifs. Have you inspected your stack or logged out these values to verify you're getting what you expect?
Page 1 of 1
« first « previous next › last »