2

I am creating a click and clear game. Once the user clicks some brick its adjacent bricks are checked for same color and all these bricks are bricks are cleared at once.

These are Cleared using clearRect() function.

Now this leaves a white patch right between the bricks above and bricks below leaving the above bricks hanging.

Now i want to bring these bricks above downward. How do i do this..? Plz help

3
  • please mention ur question more clear. If possible post sample code. Commented Aug 2, 2012 at 8:39
  • I want to create a game like this but after clicking my bricks remain in air. I want to shift them down wards Commented Aug 2, 2012 at 8:48
  • MarcoK answers your question here perfectly for how to redraw the canvas. I suggest starting another question with much more detail and your current code examples on regarding getting your "bricks" to fall. Commented Aug 2, 2012 at 9:00

3 Answers 3

11

The question is quite vague, but based on the title, you'll need to clear your canvas before you can redraw. Otherwise, the drawn elements would simply stack on top of each other.

To do this, call the function clearRect on the canvas itself:

function clear() {
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, 500, 500);
}

Where canvas is the ID of your canvas, and 500, 500 the dimensions. Now you'll have an empty canvas where you can redraw everything again.

I once created a simple HTML5 canvas game as well, you might learn from the source code.

Sign up to request clarification or add additional context in comments.

Comments

1

I think I understand what you're asking. If so then you're wanting to know how to move the blocks down when the blocks below have been removed.

This is just a matter of increasing the x position (remember the canvas starts at 0,0) with each iteration of your game loop.

How far to increase? Well that would be to where the highest "solid tower" is. I.E., say you have a column of 10 tokens and you remove the 7. The 3 below need all fall to the height of the remaining 6 - so that would be board height - (6*token height)

*
*
*
+ <- remove
* <- 6x token height (and less the board height)
*  
*  
*
*
*

Comments

0

I had success at redrawing the HTML Canvas by DOM.

    var c = document.getElementsByName("myCanvas")[0];

    if (c != null)
    {
        c.remove();
    }

    var c = document.createElement("canvas");
    c.setAttribute("name", "myCanvas");
    c.setAttribute("width", 900);
    c.setAttribute("height", 600);
    c.setAttribute("style", "border:1px solid #d3d3d3");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.