Sign in to follow this  
erich

get a group elemnt id ??

Recommended Posts

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

Share this post


Link to post
Share on other sites

forgot to attach the json file

{
 "itemData" : [
         {"item1" : 1, "key" : "item1", "type" : "apple"},
        {"item2" : 2, "key" : "item2", "type" : "apple"},
        {"item3" : 3, "key" : "item3", "type" : "apple"},
        {"item4" : 4, "key" : "item4", "type" : "apple"},
        {"item5" : 5, "key" : "item5", "type" : "apple"},
        {"item6" : 6, "key" : "item6", "type" : "apple"},
        {"item1" : 7, "key" : "item7", "type" : "apple"},
        {"item2" : 8, "key" : "item8", "type" : "apple"},
        {"item3" : 9, "key" : "item9", "type" : "apple"},
        {"item4" : 10, "key" : "item10", "type" : "apple"},
        {"item5" : 11, "key" : "item11", "type" : "apple"},
        {"item6" : 12, "key" : "item12", "type" : "apple"},
        {"item4" : 13, "key" : "item13", "type" : "apple"},
        {"item5" : 14, "key" : "item14", "type" : "apple"},
        {"item6" : 15, "key" : "item15", "type" : "apple"}
        ]
}

Share this post


Link to post
Share on other sites

I worked out a solution - I know it not uber coded but it works :)

I changed my group settings in create

    this.myItems = this.add.group();
    this.myItems.enableBody = true;
     var item;
       this.levelData.itemData.forEach(function(element){
       //create random x and y
       this.getRandX = Math.floor((Math.random() * 600) + 1);
       this.getRandY = Math.floor((Math.random() * 1000) + 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();
        item.customParams = {key : element.type, number : element.itemNumber};
        item.customIndex = addIndex;
        }, this);
    
     this.myItems.add(item);
   

in update I applied this

for (this.i = 0; this.i < this.myItems.length; this.i++){
      this.game.physics.arcade.overlap(this.myItems.children[this.i], this.item1, this.touchItem1, null, this);
       this.game.physics.arcade.overlap(this.myItems.children[this.i], this.item2, this.touchItem2, null, this);
       this.game.physics.arcade.overlap(this.myItems.children[this.i], this.item3, this.touchItem3, null, this);
       
       };

and for my custom functions I made these

touchItem1 : function(myitem, item){
        if (this.myItems.children[this.i].customParams.number == this.getRandItem1){
        //emitter
        this.item1.kill();
        }     
 },
 
 touchItem2 : function(myitem, item){
        if (this.myItems.children[this.i].customParams.number == this.getRandItem2){
        //emitter
        this.item2.kill();    
        }
 },
 
 touchItem3 : function(myitem, item){
        if (this.myItems.children[this.i].customParams.number == this.getRandItem3){
        //emitter
        this.item3.kill();    
        }
 },

json file :

{
 "itemData" : [
         {"itemNumber" : 1, "key" : "item1", "type" : "treeBobble"},
        {"itemNumber" : 2, "key" : "item2", "type" : "yellowBells"},
        {"itemNumber" : 3, "key" : "item3", "type" : "redBowTie"},
        {"itemNumber" : 4, "key" : "item4", "type" : "purpleRedCandyBar"},
        {"itemNumber" : 5, "key" : "item5", "type" : "pinkLolly"},
        {"itemNumber" : 6, "key" : "item6", "type" : "reath"},
        {"itemNumber" : 7, "key" : "item7", "type" : "blueJacket"},
        {"itemNumber" : 8, "key" : "item8", "type" : "holly"},
        {"itemNumber" : 9, "key" : "item9", "type" : "gingerBreadMan"},
        {"itemNumber" : 10, "key" : "item10", "type" : "glasses"},
        {"itemNumber" : 11, "key" : "item11", "type" : "box"},
        {"itemNumber" : 12, "key" : "item12", "type" : "globe"},
        {"itemNumber" : 13, "key" : "item13", "type" : "reindeer"},
        {"itemNumber" : 14, "key" : "item14", "type" : "scarf"},
        {"itemNumber" : 15, "key" : "item15", "type" : "snowman"}
        ]
}

It now works perfect fo me, I'm sure it could be done better but at least I found a solution, hope it helps someone else if they looking for a solution

Eric   

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.