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.

Newcomers AppGameKit Corner / [SOLVED] Sprite collision, buttons, overlaps, and "calling" scenes.

Author
Message
Kys
3
Years of Service
User Offline
Joined: 12th Nov 2021
Location:
Posted: 12th Nov 2021 02:57
I wasn't sure if I should have split these up in posts, but it's all in regards to one issue I'm having.

This is the first time coding anything in so long, assume I'm starting from scratch. (loving the programs btw!)

So I'm basically trying to create a game with multiple scenes, with multiple "buttons" that jump from scene to scene and sometimes back again. I'm almost at the 200hr mark of trying to figure this out on my own and am hoping I can get some help, probably something simple I'm missing. I've used the virtual buttons, I've used the dummy sprite with collisions option, and a few other options. But the problem seems to happen no matter how I tackle it. I'm pretty sure it's an overlapping issue where even if the sprite/button is not visible, it's still sometimes triggering an event that happened scenes ago, or should happen scenes ahead. I've tried using the depth options, no luck. Tried deleting unwanted prior buttons, scene cleanups, inactive options... etc. Sometimes I can get 15 scenes deep before things go crazy, but the most recent attempt it's happening immediately. With only a few lines to see, I'm sure there's a simple error I'm making and hope to get educated, lol.

The inst.scene has a back button (inst_back), that sits 75% over the same location as the home.scene Instructions (home_inst) button. When the game starts, I see the home screen as I should no problems, but if I click on the instructions button in any area where the back button on the next scene is located, it won't work, as I assume it's just reloading the home screen as it should?

Which brings me to the calling scenes question. I thought the "If" statement would only "do" if the "if" statement was met. So I'm not understanding why the instructions page (inst_scene) is even preloaded? Does #include actually load the entire scene in the background? I thought it was more or less just so the scene "could" be called or loaded in the future... very confused. Is there a way to delay "Including", loading, setup etc... of a scene? Even when I try to do any of that based on an if/then it's still pre loaded and my issue continues.

Thanks in advance for any help!

The author of this post has marked a post as an answer.

Go to answer

PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 12th Nov 2021 03:12
First of all, what version are you using? (Classic or Studio)

if using Studio only call the _setup() scene function when you need the scene to be shown and make sure to call the _cleanup() function when you switch

If using Classic then make sure you clean up your custom scenes.

As for the odd behaviour, yea it sounds like an overlap issue, are you using HitTest?

you really need to post some code so we can see whats going on, just your "click" and "setup" code, just something to give us a clue what could be going on, or make a vid, show us
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Kys
3
Years of Service
User Offline
Joined: 12th Nov 2021
Location:
Posted: 12th Nov 2021 03:27
Thank you,

I thought I added the screen shot, but it didn't attach. Trying again.



I am using Studio. I feel like I've tried all the setup and cleanup options in the right spots, but again, I'm not getting it maybe. I have not looked into HitTest, reading up on it as we speak!

Thanks again!
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 12th Nov 2021 03:37
GetSpriteCollision is for collision checking between 2 sprites, use GetSpriteHitTest with the mouse position to use a sprite as a button
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Kys
3
Years of Service
User Offline
Joined: 12th Nov 2021
Location:
Posted: 12th Nov 2021 03:40
So HitTest eliminates the need for the dummy sprite?
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 12th Nov 2021 03:46 Edited at: 12th Nov 2021 03:49
Yes, that's a pretty inventive way of going about it tho, I applaud your attempt

The problem is that you are creating the sprite in a loop, every frame a new sprite is created so when you click again the old dummy sprite is still there, a few mins into your game and there's 1000000+ dummy sprites floating about messing with your collisions.... NEVER create sprites in a loop like that
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 12th Nov 2021 03:47
what's the dummy sprite for?

otherwise, this might help (assuming you're using sprites for buttons):
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 12th Nov 2021 03:59 Edited at: 12th Nov 2021 04:06
hmm. scrap what i said above since there appears to be an issue on further testing:

oops. i caught the mistake. this is offered as alternative to dealing with overlapping sprites and/or those that should be (invisible? and) "inactive":

the red box should always stay "active". the white, not so much

i don't work in studio so hoping this helps?
Kys
3
Years of Service
User Offline
Joined: 12th Nov 2021
Location:
Posted: 12th Nov 2021 04:19
Thanks for both ideas, I'll try to figure them out and see what I come up with!

Thanks again for the quick replies!
Kys
3
Years of Service
User Offline
Joined: 12th Nov 2021
Location:
Posted: 13th Nov 2021 01:52 Edited at: 13th Nov 2021 01:53
Ok, so playing with the HitTest...

This worked great, IF I remove lines 33-36. Without those, the sprite button pg1_inst, functions and loads the new scene, but as soon as I added 33-36 to get back to pg1 from pg2, nothing works. Confused again. I also tried GetPointerReleased etc..

I'm still trying to figure out the second option from Virtual Nomad, it's a bit over my head, but working through it

Attachments

Login to view attachments
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 13th Nov 2021 02:09
I think, well it looks like...... they are both being called in the same loop so if pg1_inst has the same id as pg2_back then both conditions ate true at some point so one will override the other

try adding a "page_index" variable, set it page_index=1 on page one and page_index=2 on page 2 and on your "if" add "and page_index=1(or 2 depending on the page) so only one "if" can run at a time

Do you understand?

Also its better to post actual code when asking for help, this way we can directly edit your code and give instant examples... just saying, its easier to help that way
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 13th Nov 2021 02:14
Quote: " it's a bit over my head"

maybe just a tiny bit. but, consider that all sprites are set to the same sprite group by default. that's fine if you want all sprites to "collide" with each other.

the next level would be to make SOME sprites collide with SOME sprites or maybe NEVER with ANY others. maybe this example can help you understand better than mine?
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 13th Nov 2021 02:20
I think its an ID issue VN, the previous page is removed so the sprite ID will be recycled it all happens in the same frame so "if" 2 will override "if" 1reguardless of what page is set

But Virtual Nomad is right, you should get a handle on sprite groups they play a very important part of game dev especially as your games becomes more complicated
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 13th Nov 2021 05:01
thanks PTC
VN wrote: "i don't work in studio so... "

...i dont understand how studio might get in the middle of my suggestion. i might have to spend some time to find out
jd_zoo
6
Years of Service
User Offline
Joined: 12th May 2018
Location: Nova Scotia
Posted: 13th Nov 2021 18:27 Edited at: 13th Nov 2021 18:28
Not sure if this is any help or not but I have a two screen basic example using different scenes in one file...



Attachments

Login to view attachments
Kys
3
Years of Service
User Offline
Joined: 12th Nov 2021
Location:
Posted: 13th Nov 2021 21:52
Quote: "I think, well it looks like...... they are both being called in the same loop so if pg1_inst has the same id as pg2_back then both conditions ate true at some point so one will override the other"

I'm assigning the sprite buttons unique variables, as I was under the impression that was to avoid the id issues?

Quote: "try adding a "page_index" variable, set it page_index=1 on page one and page_index=2 on page 2 and on your "if" add "and page_index=1(or 2 depending on the page) so only one "if" can run at a time

Do you understand?"

I think so, I was trying "and" a few other ways with "some" success in my other project, and I assume that's another way to separate hits with specific conditions? Almost like the group idea VN suggested but different? I'll play withe index idea next!

Quote: "Also its better to post actual code..."

Do I just paste my code between:


VN your 100%, I'll be focusing on learning the grouping next/again, I think now that I've written some, all the manuals and tutorials may make more sense, I hope

I'll also try and post my main project code, with video of it running, because both using the dummysprite collision and the virtual buttons seeemed to work great for over a dozen scenes, then just randomly went wonky. (maybe that million dummysprite loop? lol)

I appreciate the test scene JD, I'll check it out too!

Again, can't thank everyone enough, help has been awesome, I'll get there, slowly.


PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 13th Nov 2021 22:10 Edited at: 13th Nov 2021 22:12
Quote: "I'm assigning the sprite buttons unique variables, as I was under the impression that was to avoid the id issues?"


The variable is just a box that holds the ID, the actual ID is generated internally and when you destroy a sprite and create another the ID's are recycled as to avoid letting the number get to high, there is an hard integer limit imposed by memory and such

so sprites create in scene 1 have ID, you destroy that scene but your variable still holds the number, you then create another scene and the ID are recycled, so now both variables for both scenes *could* hold the same value unless you also flush your variable values (myVar=-1)

to avoid this we use what's called a "finite state machine" and only process actions for the scene or game state we are currently using .... I'll code up an example in a bit.

Quote: "Do I just paste my code between:"


Yes, thats the one

Quote: "(maybe that million dummysprite loop? lol)"


I would say that's definitely the case, yes
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 13th Nov 2021 22:22
Right, this code wont work for you as its ripped from my WIP its just as an example of how to structure a finite state machine (its just a bunch of nested select statements)

It *should* be fairly self explanatory, the main loop has 5 states (the scenes) and each scene has 3 states (load, process and unload)

you see how we separate the logic from scene to scene?

Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Kys
3
Years of Service
User Offline
Joined: 12th Nov 2021
Location:
Posted: 13th Nov 2021 23:12
Yes!

Quote: "when you destroy a sprite and create another the ID's are recycled"


That exactly makes sense and explains why eventually events re-trigger deeper in the game. Thank you for explaining.
Kys
3
Years of Service
User Offline
Joined: 12th Nov 2021
Location:
Posted: 17th Nov 2021 05:37
This post has been marked by the post author as the answer.
Quick update,

With everyone's help, I think I've got it. With PTC and VN explaining the need for states and finite environments and JD's code examples, everything is working so far. I'll see if the issues deeper in the scenes begin to resurface, but so far so good!

Thanks gain!
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 17th Nov 2021 05:41
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 17th Nov 2021 06:52


Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee

Login to post a reply

Server time is: 2024-11-21 13:12:32
Your offset time is: 2024-11-21 13:12:32