DS Game Maker Logo
Home Icon

DBAS Tutorial 3 - Object in Range

Goal & Downloads

The goal is to check whether the first object (moved by the Stylus) is within range of the second. If so, display an on-screen message.

Setup

  • Add a Sprite
  • Add 2 Objects named 'Ball_1' and 'Ball_2', set the Sprite of both to the new Sprite
  • In the Room, plot one instance of each Object.

Object Movement

Open the Ball_1 Object and add a 'Touch' event (select 'Held'). Drag in the action 'Execute Code' on the 'Control' tab and paste in the following code:

  [X] = Stylus.X - ([Width]/2)
  [Y] = Stylus.Y - ([Height]/2)

This code lets the player move the first ball with the Stylus. When the Stylus is held over the Ball, it moves the Ball's center to the position of the Stylus.

Object Step

Code in the 'Step' Event happens every frame. Add a Step Event to Ball_1. Drag in the action 'Execute Code' and write the following:

  Dim NPC_ID As Integer = Get_Single_ID("Ball_2")
  Dim NPC_X as Integer = Get_X(NPC_ID)
  Dim NPC_Y As Integer = Get_Y(NPC_ID)
  Dim InRange as Boolean = false
  If [X] > NPC_X - 32 And [X] < NPC_X + 64
    If [Y] > NPC_Y - 32 And [Y] < NPC_Y + 64
      InRange = true
    End If
  End If
  Draw_Text(Top_Screen, 1, 1, "        ")
  If InRange
    Draw_Text(Top_Screen, 1, 1, "In Range")
  End If

Lots of things happen in this code. With the first line, we determine the instance ID of Ball_2. In the second and third lines, we find the X and Y positions of this instance. In the fourth line, we declare a boolean variable to store whether Ball_2 is within range of Ball_2.

The two following If statements check whether the X and Y positions of Ball_1 ([X] and [Y]) lie between boundaries aside of Ball_2 (using our own NPC_X and NPC_Y variables). If the object is in range, 'InRange' is set to true and so the line Draw_Text(...) runs to display the message. If they are not in range, no message is displayed because there is a line which clears the text off the screen (Draw_Text(Top_Screen, 1, 1, " ")) before potentially showing the message.

By Danny Felix

© 2008-2011 Danny Felix. 'DS Game Maker', its logo & 'Futur Leader' are pending trademarks of Danny Felix. 'Nintendo', 'DS', 'DSi' and 'DSi XL' are registered trademarks of Nintendo Co., Ltd.

Dark Flow Game Maker | Pro | Website Use | Privacy | Competitions

mail@dsgamemaker.com

Contact Us