If I have 5 targets created as types
ie target_1, target_2 etc
and I want to find the x,y postion of any of those targets how can I write an if statement to check the target I want
I wrote this but am unable to add a variable after 'target_'. If I want 'target_3' how can I add the '3' to the end of 'target_'?
Also how do the variables I pass into the function get returned?
Function check_target_pos(target_num,x,y)
; If target_ \x=0 End
End Function
Many thanks for reading
Graham, UK
Have a look:
type example
field x
end type
o.example=new example
o\x=50
function something()
print o\x
end function
will not work because you've declared "o" as a local (by default) and you cannot access it in functions. But you can access types using a built in list for each type... erm.. type.
If you want to individualise instances of types then it's a good idea to give them a unique ID you can use later to identify them. Regard:
type example
field x,y
field id ; unique ID (it's your responsibility to make them unique)
end type
; create two local example. These aren't accessable in functions.
o.example=new example
o\id=1
o\x=50
o2.example=new example
o2\id=2
o2\x=100
; run a program
Check_Target_Pos(1) ; try and check ID 1
waitkey
end
function Check_Target_Pos(number)
local found.example=null
found=getTypeByID(number) ; try and find 'number'
if found <> null ; check to see whether it's found it.
; do whatever you want with 'found'
; if found\x=0 then end
else
Print("Could not find example "+number)
endif
end function
function getTypeByID.example(idNumber)
; this function will cycle the list
; of examples currently created within your game
; if the example type we're cycling through
; has the same ID number as the parameter,
; it's the type we want. We return it. Note the return
; value of this function
for i.example=each example
if i\id = idNumber then return i
next
end function
Or you can do like this:
Type Target
Field X,Y
End Type
Target_1.Target=New Target
Target_2.Target=New Target
Target_1\X=5
Check_Target_Pos(Target_1,5,5)
Function Check_Target_Pos(Target_Num.Target,X,Y)
If Target_Num\X=5 Then Print("Target\x=5"):WaitKey
End Function
Yeah, or you could do it like that. ;)
;create globals
Global gfx_x=640
Global gfx_y=480
Global gfx_colour_depth=16
Global id
;==========================================================================================
;create window
Graphics gfx_x,gfx_y,gfx_colour_depth,2 ;screen =640,480,16 bit colour, always windowed
AppTitle="Shape Shoot"
SetBuffer BackBuffer()
;==========================================================================================
;create types
Type target
Field x=0 ;x pos
Field y=0 ; y pos
Field id ; id number of target
Field hit=False ;target hit/miss
Field image ;target graphic
End Type
;==========================================================================================
;create objects from types
target_1.target=New target
target_1\x=0
target_1\y=0
target_1\id=1
target_1\hit=False
target_1\image=LoadImage("target.bmp") ;x40,y40 pixels
MaskImage (target_1\image,255,255,255) ;get rid of the surrounding white of the image
;==========================================================================================
;main loop
While Not KeyHit (1)
Cls
Gosub debug_output
;draw the target on the screen
DrawImage target_1\image,target_1\x,target_1\y
;increase x and y co-ordinates of the target
target_1\x=target_1\x+5
target_1\y=target_1\y+5
;slight delay in the movement of target
Delay(100)
;check the onscreen position of target
check_target_pos(1)
;flip the buffer screen
Flip
Wend
End
;==========================================================================================
;create functions
Function check_target_pos(id)
;used to check the position of any targets on screen
Local found.target=Null
found=get_target_id(id) ; try and find 'number'
If found <> Null ; check to see whether it's found it.
Text(0,0,"found "+id)
;ok so i found that it is number 1 so how can i now say
;If target_1\x < 0 Then etc etc
;i cannot use target_id as it is not a declared type
Else
Text(0,0,"Could not find example "+id)
EndIf
End Function
Function get_target_id.target(id)
;cycle through all availible targets and return the id
For i.target=Each target
If i\id = id Then Return i
Next
End Function
;==========================================================================================
;debug
.debug_output
Text(0,400,"target 1 x="+target_1\x+" y="+target_1\y)
Return
Please read the comments in Function check_target_pos(id)
and try and help me pleeeeeaaase!
You use "found" instead. Basically, what it's doing is this:
Function check_target_pos(id)
; here, you're finding the instance of the target type
; that has the id you're looking for and putting it into
; found.
; It's the same as saying:
; j.target=new target
; a.target=null
; a = j
; You're basically pointing to it. It's not being copied,
; just being pointed to with a different name.
; This is what you're doing below. You're declaring "found"
; and then making it point to the target with
; the ID you're looking for.
Local found.target=Null
found=get_target_id(id)
If found <> Null ; check to see whether it's found it.
Text(0,0,"found "+found\id+", "+found\x+", "+found\y)
;If found\x < 0 Then etc etc
Else
Text(0,0,"Could not find example "+id)
EndIf
End Function
Check this out,
( Lol I'm to slow, some one was faster )
Function check_target_pos(id)
;used to check the position of any targets on screen
Local found.target=Null
found=get_target_id(id) ; try and find 'number'
If found <> Null ; check to see whether it's found it.
Text(0,0,"found "+id)
;ok so i found that it is number 1 so how can i now say
;If target_1\x < 0 Then etc etc
;Like This......
If found\x < 0 Then ;etc etc
Else
Text(0,0,"Could not find example "+id)
EndIf
End Function
bye...
thanks guys, its so much clearer now!!
really appreciated
If you want the X-value of a specific type-instance, there are two options:
- you add a field (i.e. InstanceNumber%) to your type
and when you want to get the X-value of a certain type, you can loop through all types of type "Target" (with First, Each, Before, After, ...) and check if the field matches the one you want the X-value from
- use arrays of types (my favorite choice, instant access, no looping needed)
Arrays are always global, so you have access to them at any time.
Like this:
Type Target
Field X%
Field Y%
End Type
dim ArrayOfTargets.Target(5) ; Creates an array with indexes 0-5 and prepares it to hold pointers to "Target"-type-instances.
; Create all instances and put the pointers into the array
For i = 1 to 5
ArrayOfTypes(i) = New Target
Next
; Now all (5) type-instances are created and each (index 1-5) index of the array points to such an instance.
; Your function:
Function check_target_pos(target_num,x,y)
; If ArrayOfTargets(target_num)\x=0 End
End Function