Jump to content

Function doesn`t work properly when the button is clicked


MANU11
 Share

Recommended Posts

Hello, I`m just getting started with game development and I`m trying to make a copy of Tetris that can be played on mobile phone as well. I want to be able to move the blocks with left/right/up arrows(from keyboard) and with 3 buttons that are displayd on the screen.

Here I`m declaring the buttons(inside create function):

create: function(){
        //....
		rotate_bricks = this.add.button(350, 180, 'rotate-shape', Tetris.Blocks.rotate, this);
		left_arrow = this.add.button(345, 240, 'left-arrow', Tetris.Blocks.moveLeft, this);
		right_arrow = this.add.button(345, 300, 'right-arrow', Tetris.Blocks.moveRight, this);
        //...
	},

And here is the moveLeft function: 

Tetris.Blocks = {
	moveLeft: function(game) {
		let points = 0;
		/*make a local variable, if all of the 4 blocks can be moved 30 px left/right
		then move it, if some of them will get out of the box, then don`t move
		Test for all blocks available on the map
		*/
		for(let i = 0; i < Tetris.allBlocks.children.length; i++) { //test if blocks collide with each others
			for(let j =0 ; j < Tetris.allBlocks.children[i].children.length; j++) {
				if(!(Tetris.allBlocks.children[i].children[j].body.velocity.y === 0) || Tetris.allBlocks.children[i].children[j].body.y > 370) {
										if(Tetris.allBlocks.children[i].children[j].body.x - 30 > 0) {
						points++;
					} else {
						break;
					}
				}
			}
		}
		for(let i = 0; i < Tetris.allBlocks.children.length; i++) { //test if blocks collide with each others
			for(let j =0 ; j < Tetris.allBlocks.children[i].children.length; j++) {
				if(!(Tetris.allBlocks.children[i].children[j].body.velocity.y === 0) || Tetris.allBlocks.children[i].children[j].body.y > 370) {
						if(points === 4) {
							console.log(Tetris.allBlocks.children[i].children[j].body.x);//for test
							Tetris.allBlocks.children[i].children[j].body.x = Tetris.allBlocks.children[i].children[j].body.x - 30;
							console.log(Tetris.allBlocks.children[i].children[j].body.x);
					} else {
						break;
					}
				}
			}
		}		
		console.log("moved left");
	},

the problem is that the console.log function is executed but the position isn`t changed(I know it works because when I use the keyboard everything is fine, I call the same function)

The variable Tetris.allBlocks is declared globally(inside Tetris.Game as: var Tetris.allBlocks = ...);

What it`s wrong? I couldn`t find an answer online

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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