Jump to content

Object next to each other


divers
 Share

Recommended Posts

Hello, recently I started creating simple game.

I want to place 4/9/.. etc object next to each other. Something like this:

[ ] [ ] or [ ] [ ] [ ]

[ ] [ ]     [ ] [ ] [ ]

            [ ] [ ] [ ]

I have an array with object cubes, but i dont know how to display them in order like this(for now they are all in the same place, 50,50).

Code for Cube:

function Cube(){
		
	this.show = function(){
			
		var temp = game.add.sprite(50,50, 'rect');
		temp.inputEnabled = true;
		temp.events.onInputDown.add(onTap, this);
			
	}
		
}

 

Link to comment
Share on other sites



EDIT:

I misread the topic, I thought @Befive.Info was asking the question and replied to add his source code... Il just leave it in case someone ever needs it. 


NEW ANSWER:

Here is another way to do it using A nested for loop (might not be as efficient, though, but it works):

    boxGroup=game.add.group();

    for(var i=0;i<13;i++){
    	for(var s=0;s<13;s++){
    		var object=makeBoxes(150,150,s,i);
    	}
    }

}

function makeBoxes(x,y,s,i){
    this.sprite=game.add.sprite(x+s*40,y+i*40,'boxes');
    boxGroup.add(this.sprite);
    return this.sprite;
}

Applied to an example:

 



OLD ANSWER(ALREADY ANSWERED BY @Befive.Info):
You can make a custom sized grid by generalizing block building to a procedure with input arguments. Here is an example of what you could do:

 

It is just A procedural approach to your box building. So you just do the exact same procedure for your other blocks, but you change the row size and keep a count of the row index. You could build the boxes as seperate groups (you have one parentgroup currently) via the procedure, so that you can customize the position of the group to your will.

You could also just simply add an if statement that checks the element in the _keyNames:

if(isAnumber(_keyNames[key])){
 _each_row_has=4;
}else{
 _each_row_has=8;
}


function isAnumber(n){
//some function that checks if it is a number
}

But the indexing will fail in this case (positions relative to letters won't look nice).
 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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