Jump to content

Overlap with groups problem


Sam987X
 Share

Recommended Posts

Hi i am quite new to phaser, but I've been making this game and in my game i have the player dropping water from a helicopter on burning buildings, the program spawns 5 buildings then adds them to the group buildings and in my update function it checks when the water is overlapping the buildings it will destroy the water, it does this but the water gets destroyed if i drop the water between buildings and also destroys it at the same height regardless of how tall the buildings are.

Any help would be much appreciated  

ps i included a picture of my game at the moment to give you an idea of what my problem is

thanks!

shorter code

function create() {
    building = game.add.sprite(20, 426, 'building1.');
    buildings = game.add.group();
//creates initial building
function SpawnBuildings(){
        
		building = game.add.sprite(building.x + Math.floor(Math.random() * 200)+200,605, 
'building'+Math.floor(Math.random()*10/2+1)+'.');
 		building.anchor.setTo(0,1)
        game.physics.arcade.enable(building);
        building.body.immovable = true;
        building.body.allowGravity = false;
		
        building.scale.setTo(.75,.75)
        buildings.add(building);
//spawns the rest of buildings

function update()
{
        if (checkOverlap(waterDrop, buildings))
		    {
			 waterDrop.kill();
		     }
}

whole project :


window.onload = function() {
    var game = new Phaser.Game(864, 648, Phaser.CANVAS,  'gameScreen', { preload: preload, create: create, update: update, render: render });
    
    function preload() {



            game.load.image('sky', 'images/sky.png', 1728, 1296);
            game.load.image('road', 'images/Road.png');
            game.load.spritesheet('player', 'images/Helicopter.png', 100, 28, 5);
            game.load.image('dirt', 'images/dirt.png', 60, 60);
            game.load.image('water', 'images/water1.png', 'images/water2.png');
            game.load.image('building1.', 'images/Buildings/1.png');
            game.load.image('building2.', 'images/Buildings/2.png');
            game.load.image('building3.', 'images/Buildings/3.png');
            game.load.image('building4.', 'images/Buildings/4.png');
            game.load.image('building5.', 'images/Buildings/5.png');
			game.load.spritesheet('fire', 'images/fire2.png', 528, 549, 2);
           

    }

    var timer;
    var total = 0;
    var player;
    var building;
    var waterDrop;
    var buildings;
	var Grounded;
	var LeftX = 0
	var RightX = 1296
    
    function create() {

        //Game Hud varriables 
            points = 0 ;
            water = 10;
            time = 50;
            count = 0;
	
        
    //level bounds
            //this.game.world.setBounds(0, 0, 1296, 600);

        //letterBox scaling fullscreen
            game.scale.fullScreenScaleMode = Phaser.ScaleManager.SHOW_ALL;
            game.input.onDown.add(gofull, this);
            function gofull() {

        if (game.scale.isFullScreen)
        {
            game.scale.stopFullScreen();
            game.state.restart();
        }
        else
        {
            game.scale.startFullScreen(false);
            game.state.restart();
        }
                this.scale.pageAlignHorizontally = true;
                this.scale.pageAlignVertically = true;

            }

        game.physics.startSystem(Phaser.Physics.ARCADE);
        buildings = game.add.group();
		waterDrops = game.add.group();
		waterDrop = game.add.sprite(-1000, 0 + 10, 'water');
        sky = game.add.tileSprite(0, 0, 1296,this.game.height, 'sky')
        dirt = game.add.tileSprite(0, 600, 1296,this.game.height, 'dirt')
        road = game.add.tileSprite(0, 600, 1296,20, 'road')
        player = game.add.sprite(100, 415, 'player');
        building = game.add.sprite(20, 426, 'building1.');
		game.world.swap(building,sky);
		game.world.sendToBack(building)
		WaterBuilding = game.add.sprite(20, 426, 'building1.');
        //buildings.add(building);
        

        game.physics.arcade.enable([player, building, waterDrop,WaterBuilding]);
		
		game.physics.arcade.enable([buildings, waterDrop]);

        game.physics.arcade.gravity.y = 200;

        var fly = player.animations.add('fly');
        player.animations.play('fly', 30, true)
        //set player image anchor        
        player.anchor.setTo(.5,.5)	
        player.body.bounce.y = 0.1;
        player.body.collideWorldBounds = true;
        player.body.setSize(70,26,-12)
        game.camera.follow(player);
        
                
        WaterBuilding.body.allowGravity = false;
        WaterBuilding.body.immovable = true;
        
        
        building.body.allowGravity = false;
        building.body.immovable = true;
        if (count < 5){
            SpawnBuildings()
        }
        if (count => 3){
            game.world.swap(buildings, sky);
            }
		
	
		
      
    //Hud text Score, water, timeLeft
            scoreText = game.add.text(20, 16, 'Score:', { fontSize: '32px', fill: '#000' });
            scoreText.fixedToCamera = true;

            waterLeft = game.add.text(700, 16, 'Water:', { fontSize: '32px', fill: '#000' });
            waterLeft.fixedToCamera = true;
        //timeLeft
            timeLeft = game.add.text(700, 66, 'Time:', { fontSize: '32px', fill: '#000' });
            timeLeft.fixedToCamera = true;
           //time left countdown
            timer = game.time.create(false);
            timer.loop(1000, TimeCountdown, this);
            function TimeCountdown() 
            {
                if (time > 0) 
                {
                    time -- 
                }
            }
            timer.start()
            EndBackground = game.add.tileSprite(0, -1296, 1296,this.game.height, 'sky')

//Keybord keys
    //arrow keys
		this.leftKey= game.input.keyboard.addKey(Phaser.Keyboard.LEFT);	
        
		this.rightKey= game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
        
		this.upKey= game.input.keyboard.addKey(Phaser.Keyboard.UP);
        
		this.downKey= game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
        
        
    //A & B button(red and blue)/A & S keys
		this.AKey= game.input.keyboard.addKey(Phaser.Keyboard.A);
		
		this.SKey= game.input.keyboard.addKey(Phaser.Keyboard.S);
        
        this.RKey= game.input.keyboard.addKey(Phaser.Keyboard.R);
		
		game.input.keyboard.addKeyCapture([ Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.A, Phaser.Keyboard.S, Phaser.Keyboard.R]);
		
 
    }
	function hitBuilding (waterDrop, buildings) {
		points =+1
	}
    function WaterDropping(){
		waterDrop = game.add.sprite(player.x, player.y + 10, 'water');
		game.physics.arcade.enable(waterDrop);
		waterDrop.body.gravity.y = 3000;
		var Water = waterDrop.animations.add('Water');
		waterDrop.scale.setTo(.5,.5)
		waterDrops.add(waterDrop);
        
        
		
	}
    
	function SpawnBuildings(){
        
		building = game.add.sprite(building.x + Math.floor(Math.random() * 200)+200,605, 'building'+Math.floor(Math.random()*10/2+1)+'.');
		building.anchor.setTo(0,1)
        game.physics.arcade.enable(building);
        building.body.immovable = true;
        building.body.allowGravity = false;
		
        building.scale.setTo(.75,.75)
        buildings.add(building);
		fire = game.add.sprite(building.x+(building.width/2),building.y-building.body.height/2,'fire');
		fire.scale.setTo(.25,.25);
		var blaze = fire.animations.add('blaze');
        fire.animations.play('blaze', 8, true);
		fire.anchor.setTo(.5,.5)
        count ++;
		
        
		}
    function RestartGame(){
		
	  	game.state.restart()

    }
	function AddScore(points){
		points =+ 10;
		
	}
	function Refill(){
		water = 10
	}
	
	
    function update()
	{
		//var LeftX = 0
		//var RightX = 1296
		this.game.world.setBounds(LeftX, 0, RightX, 600); 
        game.physics.arcade.collide(player, buildings,RestartGame);
		game.physics.arcade.collide(player, WaterBuilding, Refill)
		//game.physics.arcade.collide(waterDrop, buildings, AddScore)
		if (checkOverlap(waterDrop, buildings))
		{
			 waterDrop.kill();
			 AddScore(points)
			 points = 1
		}
		else{
			points = 0
		}
		/*if (checkOverlap(waterDrop, WaterBuilding))
		{
			 waterDrop.kill();
			 
		}
*/


  
        if (count < 4)
		{
            SpawnBuildings()
        }

        if (time == 0)
		    {
                EndBackground.y = 0
                water = 5

            }

        //update HUD text water,time, score
        scoreText.text = "Score:"+points
        timeLeft.text = "Time:"+time
        waterLeft.text = "Water:"+water


        if (water < 0)
		  {
            water = 0
          }
        
			if (this.leftKey.isDown)
		    {
				player.body.setSize(70,26,12)
				player.body.setSize(70,26,12)
				player.body.velocity.x = -150;
                player.scale.x = -1;
				if (player.angle != -20)
				{
					player.angle -= 5
				}
				
			
			}

			else if ((this.leftKey.isDown) && (player.angle == 30))
			{
				player.angle = 0
				player.body.velocity.x = -150;
                player.scale.x = -1;
				if (player.angle <= -20)
				{
					player.angle -= 5
				}
			}
		
		
			if ((this.rightKey.isDown))
			{
				player.body.setSize(70,26,-12)
				player.body.velocity.x = 150;
				player.scale.x = 1;
				if (player.angle <= 20)
				{
					player.angle += 5
				}
			}
		
		
			else if ((this.rightKey.isDown) && (player.angle == 330))
			{
				player.body.setSize(70,26,-12)
				player.angle = 0
				player.body.velocity.x = 150;
				player.scale.x = 1;
				if (player.angle <= 20)
				{
				player.angle += 5
				}	
				
			}

			if (this.upKey.downDuration(1))
			{
				LeftX =+ 1296
				RightX =+ 1296
               				
			}
		
			if (!((this.leftKey.isDown) || (this.rightKey.isDown)))
				{
				player.angle = 0;
                player.body.velocity.x = 0;
				}
            
		      function ResetAngle(player){
				player.angle = 0}
		
            if (this.AKey.isDown)
                {
                   player.body.velocity.y = -185;
                }
        
            if (this.RKey.downDuration(1))
                {
                  
				  LeftX = 0; RightX = 1296
				  this.game.world.setBounds(LeftX, 0, RightX, 600);
				  game.state.restart()
				
                }


            if ((this.SKey.downDuration(1)) && water > 0)
                {
                    water -= 1
                    WaterDropping()
							
                }


            
	}
    
    function checkOverlap(spriteA, spriteB) {

        var boundsA = spriteA.getBounds();
        var boundsB = spriteB.getBounds();

        return Phaser.Rectangle.intersects(boundsA, boundsB);
    }


    function render() 
	{
       


            game.debug.body(buildings);
            game.debug.spriteInfo(building, 32, 82);
        	//game.debug.body(fire);
            game.debug.body(player);
            game.debug.spriteInfo(player, 32, 250);
      
		//

    }
}

 

Capture.PNG

Link to comment
Share on other sites

It is a wild guess as I'm pretty new to Phaser myself but:

You're checking overlap betweens the bounds of the waterdrop and the buildings group, not each individual buildings' bounds. My suggestion is to make a loop through your buildings, or using the "overlap" method from the Arcade Physics engine.

Nice idea for a game. Cheers!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...