What's wrong here? Type-creation causing crash
BlitzMax Forums/BlitzMax Beginners Area/What's wrong here? Type-creation causing crash
| ||
Strict Graphics 800,600,0,60 Global gui:tgui = New tgui 'SetColor 255,255,255 'mainloop Repeat gui.update() gui.draw() Flip;Cls Until KeyHit(key_escape) 'draws the mouse + a rectangle Type tgui Field x=0,y=0 Field mouse:tmouse = tmouse.create() Method draw() DrawRect x,y,300,200 'mouse.draw() EndMethod Method update() 'mouse.update() EndMethod EndType Type tmouse Extends tgui Field x,y Method draw() DrawOval x,y,10,10 EndMethod Method update() x = MouseX() y = MouseY() EndMethod Function create:tmouse() Local mouse:tmouse = New tmouse Return mouse:tmouse EndFunction EndType It's probably something very basic I haven't learned. But it's bugging me quite a bit. Can someone help? I really want to have the mouse type inside the gui type so I can do gui.mouse.update() and whatnot. I also wish to have mouse extend gui, such that I can "see" the mouse from other tgui extends, for collisions and whatnot. For instance, tgui:twindow which is extended from gui, would want to see if mouse's lmb is pressed (gui.mouse.lmb) by doing 'if mouse.lmb', naturally refering to gui.mouse.lmb, but since the window is also a child of gui, it could see it.. Does that make any sense? Maybe I'm supposed to have the mouse stuff built into the tgui type for my purposes, or as a global? |
| ||
I would guess that your main problem is this line : Type tmouse Extends tgui The tgui type has a field "mouse", which calls tmouse.create()... which creates a new instance of tmouse (which is a tgui)... which has a field "mouse", which calls tmouse.create()... which creates a new instance of tmouse (which is a tgui)... which has a field "mouse", which calls tmouse.create()... which creates a new instance of tmouse... etc... ...until it crashes. :-) |
| ||
I'm not sure how having mouse extend tgui is going to benefit collision checking etc. Surely the mouse is "global", and isn't a gui object as such (like a window or a button). For collision detection, couldn't you just do some kind of bounds checking against you gui objects... starting with the biggest parents (like a window), and then working your way down to a specific child (like a button)... following the parent/child relationships - which means you are only testing against a subset of all the gui objects. just a thought :-) |
| ||
Thanks Brucey, see your point :) Now ignore for a moment the stuff doesn't compile, but is there a way to e.g. inside gui.mouse.update() to refer to the root, i.e. the parent? If I specifically want the x of the gui, but it's from inside the update function in mouse? I tried "print super.x" but that doesn't work :p Is it called something else? |
| ||
Here's an example..Strict Graphics 800,600,0,60 Global gui:tgui = New tgui gui.windowlist.addlast(twindow.create()) Repeat gui.draw() Flip;Cls Until KeyHit(key_escape) Type tgui Field x,y Field mouse:tmouse = New tmouse Field windowlist:TList = CreateList() Method draw() mouse.update() mouse.draw() For Local window:twindow = EachIn windowlist window.draw() window.checkcollide() Next EndMethod EndType Type tmouse Field x,y Method update() x = MouseX() y = MouseY() EndMethod Method draw() DrawOval x,y,60,60 EndMethod EndType Type twindow Extends tgui Field x,y,w,h Method checkcollide() 'here's the problem, how can I make it so twindow can see gui.mouse? If mouse.x > 0 And mouse.y > 0 If mouse.x > x And mouse.x < x+w And mouse.y > y And mouse.y < y+h w = 200 Else w = 100 EndIf EndIf EndMethod Method draw() DrawRect x,y,w,h EndMethod Function create:twindow(x=200,y=300,w=100,h=100) Local window:twindow = New twindow window.x = x ; window.y = y window.w = w ; window.h = h Return window:twindow EndFunction EndType I need to have the mouse coordinates callable from e.g, the windows, in the gui's window lists. How is that done? I thought it was supposed to work when I did it this way. The mouse is just a child, it's not extended from gui, so why can't I do mouse.x etc. inside a method of a gui-extend? |
| ||
The problem is Field mouse:tmouse = New tmouse in your tgui. It means each instance of a gui object has its own (non-updating other than the top gui object) mouse object. what if you change it to : Global mouse:tmouse = New tmouse Then all gui instances "share" the same mouse... ? |
| ||
So just a global inside the type instead of a field? It's really that simple? Wow :p Thanks! |