Question about Mouse in DelphiX

Started by
6 comments, last by Delpher 22 years, 9 months ago
Hello, Please help me with the following : For example in my DelphiX game I have a sprite with coordinates : X:=100; Y:=100; The resolution is 800x600, there is DXInput in my application. So the question is : how should I describe the event of clicking on my sprite with the mouse. You know like clicking on a soldier in warcraft or starcraft. Thank you in advance.
Advertisement
Well that depends, there are various ways it could be accomplished. Probably the most accurate way would be to do a collision check with the Mouse''s X and Y coordinates. Basically all you''re doing is checking whether or not the current mouse click was within a sprite''s rectangle, and if so what sprite it was, and then do what needs to be done. You could write a quick procedure to perform this (looping through all the sprites and checking X and Y mouse coordinates), or you might consider using the TImageSprite''s inherant DoCollision event (invisible 1x1 sprite, would be needed to mirror the mouses actions though). I''m not sure you need DirectInput for this though, when I made a level editor for one of my games I simply used the TDXDraw''s OnMouse events (of which the Mouse X and Y are relative and confined to the TDXDraw object). Let me know if you still need more help.
The brute force method is to loop through all objects in your game and check if the mouse coordinates are within the bounds of each object. The first object whose bounds contain the mouse coordinate is the object that was selected.
type  PSprite = ^TSprite;  TSprite = record    X, Y: Integer;    Width, Height: Integer;  end;function GetSpriteByCoords(X, Y: Integer): PSprite;var  Index: Integer;begin  Index := 0;  while Index < FObjects.Count do  begin    Result := PSprite(FObjects[Index]);    if (X >= Result^.X) and (X < (Result^.X + Result^.Width))      and (Y >= Result^.Y) and (Y < (Result^.Y + Result^.Height)) then      Exit;    Inc(Index);  end;  Result := nil;end;

There are various ways you can optimize this, such as keeping a separate list of only those objects that are actually on-screen and keeping that list sorted from top of screen to bottom of screen. When checking the list of objects for a mouse click, start at the bottom of the screen (end of the list) and work your way up the screen (backwards through the list). This accounts for the fact that a sprite lower on the screen is in front of a sprite higher up the screen. Therefore if the two sprites overlap and you click on the overlapping area, it will automatically find the sprite that is in front.
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
Sorry but I need some more help...

So I deceided to use the trick with invisible sprite, which represents mouse, and collision test. Everything was okay untill I scrolled my map. The problem is, that when the map is scrolled in any direction, the invisible sprite stays at it''s X and Y, and when the mouse is moved again, the REAL cursor and the invisible SPRITE do not match (their coordinates I mean, heh).

For scrolling I use the following :

if isRight in MainForm.DXInput.States then
X := X + (300/1000)*MoveCount;
end;

Engine.X := -X+Engine.Width div 2-Width div 2;
Engine.Y := -Y+Engine.Height div 2-Height div 2;

For determening the position of invisible sprite I use this :

CursorSprite.X:=Mouse.CursorPos.X;
CursorSprite.Y:=Mouse.CursorPos.Y;

When the mouse is moved, I use TDXDraw.OnMouseMove;



Hope you can help me



Tisk tisk, you should be ashamed of yourself, this is an easy one.

All you need are two new variables to track how much X and Y scrolling has been done. Then you just add that value to your pseudo-mouse sprite coordinates.

CursorSprite.X := Mouse.CursorPos.X + XScroll;
CursorSprite.Y := Mouse.CursorPos.Y + YScroll;

Edited by - Xorcist on July 3, 2001 5:20:16 PM
Not to get off topic, but speaking about scrolling, does anyone know where I can get a good tutorial on performing scrolling with DelphiX? Maybe one that has some parallax scrolling in it. I''ve come across plenty of open source projects, but they''re all too large scale and I want a tutorial that just covers scrolling.
Try the Mappy library. Michael and myself just did a conversion of the Mappy headers for Delphi. Michael did some samples using both DelphiX and UnDelphiX.

Mappy home page

Delphi import unit

Note that the Delphi import unit is still in beta. If you create a better sample or find some problems, please tell us in this forum.

Steve ''Sly'' Williams  Code Monkey  Krome Studios
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
Hey Mappy looks sweet! I just downloaded it. Saves me the trouble of having to build my own level editor (once I get to my side scroller project). I have to finish up some of my arcade conversions first.

{By the way, what is this UnDelphiX I keep hearing about}

Edited by - Xorcist on July 3, 2001 8:39:27 PM

This topic is closed to new replies.

Advertisement