Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Check if the mouse button is pressed down

    2D Blocks
    1
    1
    316
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

      Introduction

      You can check if any mouse button is pressed down on the stage window using the following boolean block in the sensing category:

      If the mouse button is pressed down and the pointer is inside the stage window at the moment this block is played, then it will report “true”; otherwise, it will report “false”.

      p.gif

       
       

      A Common Mistake

      One common mistake is to check for the mouse press in a single “if” block like this:

      p.gif

      This won’t work because the program will run through the “if” and “mouse down?” blocks very quickly when you click the green flag button. Therefore, if you press down the mouse button after clicking the green flag, the program will not respond to you, because it has already ended.

       
       

      Combined with Forever + If

      Most of the time, the correct way is to use the “mouse down?” block together with a forever block and an “if” block. This way, the program will repeatedly check if the mouse is pressed down.

      For example, we can turn the ball when the mouse is pressed down like this:

      p.gif

       
      Note that you can press down the mouse anywhere on the stage to make this block report true. You do not need to click on the sprite.

      1 Reply Last reply Reply Quote 0
      • First post
        Last post