GameBuilder develops the 100-line code of the game application series to implement aircraft hitting and gamebuilder100 lines
Online Preview: http://osgames.duapp.com/apprun.html? Appid = osgames1-951421981197090
Online Editing: http://osgames.duapp.com/gamebuilder.php? Appid = osgames1-951421981197090
SCAN:
It is not difficult to implement aircraft logging. The knowledge points to be used are as follows:
- Collision Detection: it is not detected between the enemy and the enemy. Bullets and the enemy must be detected.
- Touch follow: our plane will change according to the position of the touch point.
- Bullets and enemy planes: bullets need to be fired at regular intervals, and enemy planes are randomly generated.
- Background scrolling: you cannot see that the background image is spliced.
Collision Detection
Collision can only happen between the rigid body and the rigid body. The plane here is implemented by Frame Animation. Therefore, you need to place the Frame Animation in the rigid body as a child control of the rigid body. Bullets are images and must be placed in a rigid body.
If you want to avoid collision between rigid bodies of the same type, you only need to setGroupIt can be the same plural.
Touch follow
- Touch, press, move, lift, there will be a corresponding event, here you only need to handle the movement event, according to the position of the touch point, adjust the location of the plane.
The Code is as follows:
var win = this.getWindow();var me = win.find("my-plane");me.setPosition(point.x - 0.5 * me.w , point.y - 0.5 * me.h);
Generate bullets and enemy planes
- The bullets should be continuously pushed forward. Therefore, a timer is required and a custom UITimer control is provided in the GameBuilder. As long as the control is dragged into the window, it can be used. The scheduled cycle and frequency are configured in the special attribute of the timer, and the relevant code can be written into the onTimeout event.
- Use the Math. random () function provided by javascript to determine the location of the enemy server.
Rolling background
In the scenario, you can set the virtual width/height to be greater than the window width/height, set the background image to horizontal/vertical tile, and then set attributes for the Rigid Body.Follow meYou can achieve the rolling effect of the scenario following the main character. For example, see the demo by Dr. Li xianjing.
However, I did touch follow here, and the plane was random during motion. Therefore, it is not suitable to set the camera to follow me. In this case, the scene will shake up and down a little strange.
Another method I use here is to use three images and a timer. The onTimeout Event code of the timer is as follows:
var win = this.getWindow();var bg1 = win.find("bg-1");var bg2 = win.find("bg-2");var bg3 = win.find("bg-3");var h = bg1.getHtmlImage().height;bg1.setPosition(0, win.offset);bg2.setPosition(0, win.offset - h);bg3.setPosition(0, win.offset - 2*h);win.offset += 2;win.offset %= 2*h;
Although it is not difficult to implement the overall framework, there are still many points to be optimized, such as aircraft acceleration control, aircraft random position control, and aircraft quantity control, to be reserved for interested friends.