Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / pointer on a grid

Author
Message
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 6th Feb 2013 14:04 Edited at: 6th Feb 2013 14:28
how do i make the x and y coordinates of the pointer stick to a grid so that it moves a 8*4 block at a time,basically in like the one used in AGK's own object placement editor with the grid turned on, thanks for any help

Hail to the king, baby!
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 6th Feb 2013 18:34
Simple enough, I do it regularly with editors.

That will position the sprite at the grid position of the cursor, which is set to 10 x10. You can play with the grid size to get it how you want, add an offset if you do not want it to start at the edge of the screen etc.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th Feb 2013 18:46
and the same as a function

Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 6th Feb 2013 23:26
Quote: "...a 8*4 block ..."

If the size is fixed and a power of 2, you could use a short cut;

Done in two steps to force to integer before the binary AND (&&).

(I don't know how AppGameKit would handle binary maths on the float returned from the pointer function)

nb.
-8 = %11111111111111111111111111111000
-4 = %11111111111111111111111111111100

So basically it ignores the lower bits and so moves in steps of 8 and 4.

Only works with numbers which are powers of 2
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 7th Feb 2013 00:05
Marl, since the variables you used in your snippet are all integers by default (and Tier 1 AppGameKit will recast the float values from the get pointer commands to integers), it should work. But it should be written this way, to make sure the operator precedence works the desired way:


We haven't had anyone official weigh in about the operator precedence used in the AppGameKit Tier 1 compiler. In general, most languages honor negation over bitwise operations.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 7th Feb 2013 10:45
well..this is more than enough!
thanks
i was trying to divide and multiply somewhere before I asked for help but my code got all messy, had to totally recode the project but this time putting in plenty of rem blocks around the functions/vars so I don't get lost again!

Hail to the king, baby!
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 7th Feb 2013 12:02 Edited at: 7th Feb 2013 12:50
I know this repost is only related by the fact that it is still about the editor i am trying to write, but could you please tell me the best way to ensure that i dont place more than one sprite at the current pointer location? this is the code I have so far to give you a grasp of how it is setup:





I have included this zip of the project file if anyone is interested

thanks again for any help
Modified the code slightly in the zip but only added to the reset function to clear the brick type fields




Hail to the king, baby!

Attachments

Login to view attachments
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Feb 2013 12:54
before you create the sprite look at your array if there is a imagenumber.
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 7th Feb 2013 12:56
i was thinking that,but i dont have any real track of where it is in the array?

Hail to the king, baby!
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Feb 2013 13:13 Edited at: 7th Feb 2013 13:15
here at "Place a brick at current pointer location"

for next and compare

for n = 1 to bricklimit
if brick[n].xloc = x1*gridx and brick[n].yloc = y1*gridy and brick[n].imagenum=0
//Add Sprite
exit
endif
next

(if you use a array[x,y] as BrickType you don't need to search.)
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 7th Feb 2013 13:21
tried adding the code





it doesnt seem to want to put any sprite down..heh

I was trying to avoid for next loops and was looking into collisions via grouping instead but it never worked out for some reason

Hail to the king, baby!
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Feb 2013 13:45
hmmm,
try with a print("yes") after if and make a else print("no") for debug

print this at screen
x1*gridx
y1*gridy
brick[n].xloc
brick[n].yloc
brick[n].imagenum
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 7th Feb 2013 13:51 Edited at: 7th Feb 2013 13:55
brrrr!

would help if it had a debug console on runtime!


i have tested x1*gridx and y1*gridy, they simply start at 0,0 top left and changes as they should,in increments of 8 and 4 respectively

not sure about printing the others in a for next loop though!


just added this




if i have it right? anyhow it prints NO!! when i try to put one down

Hail to the king, baby!
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 7th Feb 2013 14:01 Edited at: 7th Feb 2013 14:39
ok i have done the rest




the last 3 values print zero..which is kinda wrong for the xloc and yloc as i was in the middle of the screen when i pressed... I have attached the full project as it stands


if you rem out the checks like so:







it will paste at will..all be it over other sprite, and saves/loads correctly( btw the top left button is to load, the one underneath that is to save, and the top right button clears the level, just thought i would say as i have yet to make the gfx for the buttons!)

Hail to the king, baby!

Attachments

Login to view attachments
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 7th Feb 2013 14:57 Edited at: 7th Feb 2013 14:59
i suspect it is the fact that it is in a for next loop, the check could be simply missed depending on where then next 'n' is at the time of the check

Hail to the king, baby!
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 7th Feb 2013 16:23 Edited at: 7th Feb 2013 16:24
You could simply do a check for sprite collision at your pointer co-ordinates using getspritehit(x,y). If there is a sprite underneath the cursor do not place the tile. The spritehit command also gives you the relevant sprite number, so you could specify a range and also use it to delete sprites.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Feb 2013 17:26 Edited at: 7th Feb 2013 17:49
theory and practice

shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 7th Feb 2013 17:58
ah,splendid, thanks markus,i was messing about with some flag before, then i messed around with the collision stuff before looking here again, but that will do.
thanks

Hail to the king, baby!
Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 7th Feb 2013 18:51 Edited at: 7th Feb 2013 18:52
A bit of optimisation

pre-calculating the terms used for comparison means the multiply is only done once rather than once per loop.

And since the loop variable n is incremented and compared at the next n instruction, n will always be higher than bricklimit on exiting the loop if no match is found, so You don't need the flag;

Login to post a reply

Server time is: 2024-11-24 08:51:35
Your offset time is: 2024-11-24 08:51:35