Search the Community
Showing results for tags 'element'.
-
Hi I have a script (below): How do I get the elements for myItems, I want to drag an item over an identical item placed at the top of the stage so that I can kill it here is my custom function once there is an overlap touchItem1 : function(myitem, item){ console.log('MY ITEM ->' + this.myItems.key + ' - RANDOM ITEM KEY -> ' + this.getRandItem1); if (this.myItems == this.getRandItem1){ console.log('Touching the same id key and now i can kill the sprite'); } }, link : http://html5gamer.mobi/phaser2/santa/ var FreestyleGame = FreestyleGame || {}; FreestyleGame.GameState = { //initiate game settings init: function(currentLevel) { //use all the area, don't distort scale this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; //cursor keys this.cursors = this.game.input.keyboard.createCursorKeys(); //initiate physics system this.game.physics.startSystem(Phaser.Physics.ARCADE); //game constants this.PLAYER_SPEED = 500; this.score = 0; }, create: function() { //moving stars background this.background = this.add.sprite(0, 0, 'background'); this.levelData = JSON.parse(this.game.cache.getText('level')); // create 3 top irems to choose from this.getRandItem1 = Math.floor((Math.random() * 15) + 1); this.getRandItem2 = Math.floor((Math.random() * 15) + 1); this.getRandItem3 = Math.floor((Math.random() * 15) + 1); // show 3 random items at top this.item1 = this.add.sprite(this.game.width / 2 - 200 ,100,'item' + this.getRandItem1); this.item1.anchor.setTo(0.5); console.log(this.getRandItem1); this.game.physics.arcade.enable(this.item1); this.item2 = this.add.sprite(this.game.width / 2 ,100,'item' + this.getRandItem2); this.item2.anchor.setTo(0.5); this.game.physics.arcade.enable(this.item2); this.item3 = this.add.sprite(this.game.width / 2 + 200,100,'item' + this.getRandItem3); this.item3.anchor.setTo(0.5); this.game.physics.arcade.enable(this.item3); //my items this.myItems = this.add.group(); this.myItems.enableBody = true; var item; this.levelData.itemData.forEach(function(element){ //create rendom x and y this.getRandX = Math.floor((Math.random() * 711) + 1); this.getRandY = Math.floor((Math.random() * 1111) + 200); //get info from the json file item = this.myItems.create(this.getRandX,this.getRandY, element.key); // get key //touch and drag item.inputEnabled = true; item.input.enableDrag(); }, this); this.myItems.add(item); //player this.player = this.add.sprite(500, this.game.world.height - 200, 'player'); this.player.anchor.setTo(0.5); this.game.physics.arcade.enable(this.player); this.player.body.collideWorldBounds = true; this.player.customParams = {}; this.createOnscreenControls(); //load level this.loadLevel(); this.addScore = this.game.time.events.loop(Phaser.Timer.SECOND * 4, function(){ this.score ++; this.distance --; this.refreshStats(); }, this); }, update: function() { this.myItems.forEach(function(element){ //console.log(element.key); }, this); this.game.physics.arcade.overlap(this.myItems, this.item1, this.touchItem1, null, this); this.game.physics.arcade.overlap(this.myItems, this.item2, this.touchItem2, null, this); this.game.physics.arcade.overlap(this.myItems, this.item3, this.touchItem3, null, this); //listen to user input if(this.game.input.activePointer.isDown) { //get the location of the touch var targetX = this.game.input.activePointer.position.x; //define the direction of the speed var direction = targetX >= this.game.world.centerX ? 1 : -1; //move the player this.player.body.velocity.x = direction * this.PLAYER_SPEED; } // keyboard and touch control if (this.cursors.left.isDown || this.player.customParams.isMovingLeft){ this.player.body.velocity.x = - this.PLAYER_SPEED; } else if (this.cursors.right.isDown || this.player.customParams.isMovingRight){ this.player.body.velocity.x = + this.PLAYER_SPEED; } }, createOnscreenControls : function(){ this.leftArrow = this.add.button(this.game.world.width * 0.05,this.game.world.height /2, 'arrowButton'); this.rightArrow = this.add.button(this.game.world.width * 0.95,this.game.world.height /2, 'arrowButton'); this.leftArrow.alpha = 1; this.leftArrow.scale.setTo(2); this.rightArrow.alpha = 1; this.rightArrow.scale.setTo(2); this.rightArrow.scale.x = -2; //left this.leftArrow.events.onInputDown.add(function(){ this.player.customParams.isMovingLeft = true; }, this); this.leftArrow.events.onInputUp.add(function(){ this.player.customParams.isMovingLeft = false; }, this); this.leftArrow.events.onInputOver.add(function(){ this.player.customParams.isMovingLeft = true; }, this); this.leftArrow.events.onInputOut.add(function(){ this.player.customParams.isMovingLeft = false; }, this); //right this.rightArrow.events.onInputDown.add(function(){ this.player.customParams.isMovingRight = true; }, this); this.rightArrow.events.onInputUp.add(function(){ this.player.customParams.isMovingRight = false; }, this); this.rightArrow.events.onInputOver.add(function(){ this.player.customParams.isMovingRight = true; }, this); this.rightArrow.events.onInputOut.add(function(){ this.player.customParams.isMovingRight = false; }, this); }, touchItem1 : function(myitem, item){ console.log('MINE ->' + this.myItems.key + ' - RAND -> ' + this.getRandItem1); if (this.myItems == this.getRandItem1){} console.log('Touch'); }, touchItem2 : function(myitem, item){ console.log('MINE ->' + this.myItems.key + ' - RAND -> ' + this.getRandItem2); if (this.myItems == this.getRandItem1){} console.log('Touch'); }, touchItem3 : function(myitem, item){ console.log('MINE ->' + this.myItems.key + ' - RAND -> ' + this.getRandItem3); if (this.myItems == this.getRandItem1){} console.log('Touch'); }, }; thanks in advance Eric
-
Hello everyone! In my aplication i have done phaser game wich is cooperate with droopable and draggable gui. When i dropped element from equipment div to div with phaser game screen, console allert me element id from data base. Now i want to draw element in phaser game screen when i dropped it on screen div but dont have ide how. How to add elements to phaser game from java script function? Anyone have idea? How it work You can see on page http://lifetime.cba.pl when You login Login: test, password test, and them klick "graj" Maybe anyone can help Greets.