Breakout Clone
jem170884

Hiya,

I am working my way through the breakout tutorial and I am having trouble cloning my block template.

I have the following code

// Create Blocks
    var blockTypes = ['72248.jpg', '72811.jpg', '72903.jpg', '72996.jpg', '73184.jpg', '73221.jpg', '73269.jpg', '73405.jpg', '73450.jpg'];
    for(var i=0; i<4; i++);
    {
        for(var j=0; j<3; j++)
        {
            var newBlock = wade.getSceneObject('TemplateBook').clone();
            newBlock.setPosition((i-1)*75, -250 + j*115);
            wade.addSceneObject(newBlock, true);
            var type = blockTypes[Math.floor(Math.random()*4)];
            newBlock.getSprite().setImageFile(type);
        }
    }
});

I can't seem to get 4 blocks in a horizontal row, it shows only 1, however the vertical blocks are showing as desired (3 blocks in a column).

Can you advise where I am going wrong?

All 5 Comments
foxcode

Hi jem. I cannot see anything obviously wrong with it. If you export your project as a zip and add it here as an attachment I might be able to take a look at it. I suspect the problem is somewhere else though, that code looks fine I think

Edit: I would maybe add console.log((i-1)*75, -250 + j*115); Inside your inner for loop just to make sure the values are correct

jem170884

Thanks Foxcode. Have sent you a private message.

foxcode

Thanks for sending the code

Can't believe I didn't spot this on my initial look

  for(var i=0; i<4; i++)

Note the semi colon at the end of the line. Remove that semicolon. I realise it is a typo but I think it is causing only the inner loop to run, so you only get 3. A simple console log showed that the inner loop was only being run 3 times, not 9 or 12 as you intended.

I will check out the collision and reply again on this message

foxcode

Okay.

Your collision code is correct. As you say it works on the left and right borders.

Your scene has a size of 540 by 960, you can check this in the scene properties. If my memory is correct this is the size I normally use in the tutorials.

However, your background image on the play page is 540 by 640.

To solve your problem either make your background sprite the same size as the scene, or better yet use a half height value based on your sprite

Instead of
var halfHeight = wade.getScreenHeight()/2;

Try
var halfHeight = wade.getSceneObject("Backgroun").getSprite().getSize().y/2;

I made these changes on my version and it seemed to work, hope this helps

 

jem170884

You're a star. Had worked out the collision but just couldn't see the error with the cloning.

Post a reply
Add Attachment
Submit Reply
Login to Reply