Hi all. I am struggling hard. I would like to know how I make an item kill my character.
Here is my source code:
Caliban's Great Adventure<script src="js/phaser.min.js"></script>
<!--Phaser states
Boot: start the physics,
Load: Preloader for overall game - before menu
Menu: The main menu for game
Play: Your actual game
Win: You the winner, play again button
Game: Where you define what all the states are called-->
<script src="js/load.js"></script>
<script src="js/menu.js"></script>
<script src="js/play.js"></script>
<script src="js/win.js"></script>
<script src="js/lose.js"></script>
<!--always put the game state last because it has the new phaser game creation here. That also defines all the states-->
<script src="js/myGame.js"></script>
<h1>Caliban's Great Adventure</h1>
<div id="gameTime"></div>
<h5>"We'll swear upon that bottle to be thy true subject;
for the liquor is not earthly." -Caliban, The Tempest
<p><img src="assets/calibanweb.jpg"></img>
Caliban leaves it's home to venture out and find "moonjuice". Be careful though, there are lots of obstacles in its way.
</p>
How To Play:
W to start
Respective Arrow Keys to move left, right or up.
Here is my load code:
// JavaScript Document
// Load State
var loadState={
preload: function(){
//visuals to tell people assets are loading
game.add.text(80,150,'Loading...',{fill:'#FFF'});
//preload the little dude - syntax for loading spritesheet
//id, path to the asset, width of the frame, height of the frame
game.load.spritesheet('Caliban', 'assets/Caliban.png', 32, 32);
//load in our collectable
//id, path
game.load.image('Jug', 'assets/Jug.png');
//BAD GUY BOO
game.load.image('Gon','assets/Gon.png');
//load my map - json file & tileset
//load json object holds the placement of tiles
//id, path, something I'm not sure, type of file
game.load.tilemap('myMap', 'assets/TileMap.json', null, Phaser.Tilemap.TILED_JSON);
//uses the tilesheet for graphics
game.load.image('tiles', 'assets/Tilemap.png');
game.load.image('planets', 'assets/Planets.png');
},
create: function(){
game.state.start('menu');
}
}
And here is my play code:
var player;
var map;
var layer;
var cursors;
var items;
var hurt;
var score=0;
var scoreText;
var playState={
create: function(){
/***************************All code to set up the scene*************************/
//set background colour
game.stage.backgroundColor = "#456083";
/*****************************************map************************************/
//adds a tilemap to the game
map = game.add.tilemap('myMap');
//make the connection between the map and the pictures
//first is name of tileset in json file, id in the preload of tiles
map.addTilesetImage('Tilemap', 'tiles');
map.addTilesetImage('Planets', 'planets');
//this is how to load a static layer - the player doesn't interact with
map.createLayer('clouds');
layer=map.createLayer('Platforms');
layer.resizeWorld();
//telling the game that we want to collide with platforms
//first tile, last tile, true, id of layer
map.setCollisionBetween(0, 64, true, 'Platforms');
/***************************************player***********************************/
//dropping our character on the screen
//x, y, id
player = game.add.sprite(100,100, 'Caliban');
//add some physics
game.physics.arcade.enable(player);
player.body.gravity.y=250;
player.body.bounce.y=0.2;
//define the moving animations
//name animation, which frame should it play, fps
player.animations.add('left', [0,1,2,3], 5, true);
player.animations.add('right', [5,6,7,8], 5, true);
//FOLLOW OUR PLAYER
game.camera.follow(player);
/********************************create collectables*****************************/
items = game.add.group();
//give it a 'body' so that we can actually collide with it
items.enableBody=true;
//game create - x, y, id
items.create(460,235,'Jug');
items.create(700, 650,'Jug');
items.create(1535,100,'Jug');
items.create(1440,550,'Jug');
items.create(1490,325,'Jug');
//create more items here
hurt = game.add.group();
//game create - x, y, id
hurt.create(500, 670,'Gon');
hurt.create(1485, 550,'Gon');
hurt.create(1440, 130,'Gon');
hurt.create(1298, 130,'Gon');
hurt.create(800, 260,'Gon');
hurt.create(1008, 700,'Gon');
//create more items here
/*********************************Create Audio***********************************/
/******************************Add Score*************************************/
scoreText = game.add.text(20, 20, 'Score:0',{fill:'#71BD64'});
scoreText.fixedToCamera=true;
/**********************************start tracking keys****************************/
cursors = game.input.keyboard.createCursorKeys();
},
update:function(){
/************All code that needs to be checked regularly*************************/
/*********************check if player hits platforms*****************************/
var hitPlatforms = game.physics.arcade.collide(player, layer);
/***************player movement AFTER cursors have been created******************/
if(cursors.left.isDown){
console.log("left");
player.body.velocity.x=-150;
player.animations.play('left');
}else if(cursors.right.isDown){
console.log("right");
player.body.velocity.x=150;
player.animations.play('right');
}else{
//stop moving - velocity - 0
player.body.velocity.x = 0;
//play frame 4
player.frame=4;
}
//jumping
//allow the player to jump if it is touching the ground AND the up key is pressed
if(cursors.up.isDown && hitPlatforms){
player.body.velocity.y=-215;
}
/***************************check if player hits items************************/
//var1, var2, function call, null, this
game.physics.arcade.overlap(player, items, this.collect, null, this);
/**************************SCORE END SCREEN TRIGGER*******/
if(score==50){
this.win();
}
},
collect:function(player, item){
/************function for what happens when player hits diamonds******************/
console.log("They are hitting");
item.kill();
//increment the score
score+=10;
//then put it in the text field
scoreText.text='Score:'+score;
},
win:function(){
/************function for what happens when score reaches certain amount**********/
//console.log("win");
game.state.start('win');
}
}
I would like to make it so my object ‘Gon’ kills my player, “Caliban” when they touch.
I have no idea how to do this!!
I also have an external js file linked to my source code called “lose.js” but nothing has been done with it yet.
I am extremely new to phaser and am surprised I have made it this far. Now I am stumped!!!
TIA!