Zho:子弹的发射

This tutorial explains how to create the effect of shooting, launching, or throwing an object. The code in this article does not simulate velocity or parabolic motion, but to a projectile that, after the initial force, is not acted upon by any traditional force (gravity, wind, etc.), though it may be obstructed by walls or targets.

In each example, the projectile shoots when the space key is pressed, but the trigger can be any 菱形积木.

Two sprites, no cloning
These scripts require two sprites; a player sprite and a projectile sprite. No 分身 is used.

Semi-Automatic
This 程式 requires the player to release the trigger button before firing again. This reduces the rate of fire:

當 @greenflag 被點擊 // in the player's character sprite 重複無限次 等待直到 <[空白 v] 鍵被按下？> 廣播訊息 [shoot v] 並等待 等待直到 <<[空白 v] 鍵被按下？> 不成立> // the player can only shoot once at a time end

當收到訊息 [shoot v] // in the projectile sprite 定位到 [You v] 位置 // start at the player 顯示 重複直到 <<碰到 [intended target(s) v] ?> 或 <碰到 [邊緣 v] ?>> 移動 (10) 點 end // moves the sprite until it is touching something that will make it go away 隱藏

Automatic
This script allows the player to simply hold the trigger button for continuous fire:

當 @greenflag 被點擊 // in player's sprite 重複無限次 等待直到 <[空白 v] 鍵被按下？> 廣播訊息 [shoot v] 並等待 等待 (0.1) 秒 // does not wait for the trigger to be released; change to amount of seconds between each shot end

當收到訊息 [shoot v] // in projectile sprite 定位到 [You v] 位置 顯示 重複直到 <<碰到 [intended target(s) v] ?> 或 <碰到 [邊緣 v] ?>> 移動 (10) 點 end 隱藏

Two sprites, one cloning the other
This method is similar to the one above, only it incorporates cloning to allow for more than one bullet on the screen at once. Once again, both a player sprite and a projectile sprite are needed.

Semi-Automatic
當 @greenflag 被點擊 // in player sprite 重複無限次 等待直到 <[空白 v] 鍵被按下？> 分身 [bullet v] 建立 // clones the projectile sprite 等待直到 <<[空白 v] 鍵被按下？> 不成立> end

當分身產生 // in projectile sprite 重複直到 <<碰到 [intended target(s) v] ?> 或 <碰到 [邊緣 v] ?>> 移動 (10) 點 end 分身刪除

Automatic
當 @greenflag 被點擊 // in player sprite 重複無限次 等待直到 <[空白 v] 鍵被按下？> 分身 [bullet v] 建立 // clones the projectile sprite 等待直到 <<[空白 v] 鍵被按下？> 不成立> end

當分身產生 // in projectile sprite 重複直到 <<碰到 [intended target(s) v] ?> 或 <碰到 [邊緣 v] ?>> 移動 (10) 點 end 分身刪除

One sprite cloning itself
This method is more efficient because it only requires one sprite with two costumes: the player and the projectile.

Semi-Automatic
當 @greenflag 被點擊 造型換成 [player v] 重複無限次 等待直到 <[空白 v] 鍵被按下？> 分身 [自己 v] 建立 等待直到 <<[空白 v] 鍵被按下？> 不成立> end

當分身產生 造型換成 [bullet v] // makes the clone look like a projectile 重複直到 <<碰到 [intended target(s) v] ?> 或 <碰到 [邊緣 v] ?>> 移動 (10) 點 end 分身刪除

Automatic
當 @greenflag 被點擊 造型換成 [player v] // makes sure the parent sprite looks like the player 重複無限次 等待直到 <[空白 v] 鍵被按下？> 分身 [自己 v] 建立 等待 (0.1) 秒 // change to amount of seconds between each shot end

當分身產生 造型換成 [bullet v] // makes the clone look like a projectile 重複直到 <<碰到 [intended target(s) v] ?> 或 <碰到 [邊緣 v] ?>> 移動 (10) 點 end 分身刪除

Tips
面朝 [滑鼠游標 v] 向
 * To make the projectile aim at your mouse pointer, place the following block after the "go to x: y: " 积木:
 * If the length of the sprite is less than 10 pixels, replace both 10's with at most the length of the sprite, or the sprite may pass through its intended targets.
 * You can also change the length of the sprite's costume so that it will still detect the intended targets.