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 / i would like to make a sprite a botton how can i?

Author
Message
kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 25th Oct 2011 21:05
i was trying to make a sprite a button so when it is pressed A spesific action takes place but could not do this it just made the whole screen the button can anyone please help
many thanks

kirtnicholls
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 25th Oct 2011 21:35 Edited at: 25th Oct 2011 21:36
This might not be the best way, but here's what I'm doing...




I have a sprite with it's ID in randomizeSpr, and two images for it, one with a "button up" graphic, one with a "button down" graphic. Then I do a check for GetPointerReleased()...



Reactor
15
Years of Service
User Offline
Joined: 6th Apr 2009
Location:
Posted: 21st Dec 2011 17:18
Have you tried using the AddVirtualButton button function, it will do the work for you...

You can customise the button with functions like:

SetVirtualButtonSize
SetVirtualButtonPosition
SetVirtualButtonImageUp
SetVirtualButtonImageDown


You can get the state of the button with functions like:

GetVirtualButtonState
GetVirtualButtonPressed
GetVirtualButtonReleased


There are examples in the examples/input/virtualbuttons folder

Hope this helps

==================================================
you gotta live to survive to be a hundred and five
==================================================
westalke
12
Years of Service
User Offline
Joined: 8th Dec 2011
Location:
Posted: 22nd Dec 2011 13:02
The frustrating weakness with Virtual buttons is the lack of tristate support for Enabled / Disabled buttons.

The addition of SetVirtualButtonEnabled and GetVirtualButtonEnabled plus a way too set a disabled image using SetVirtualButtonImageDisabled would make it a very useful too.

Westa
LeeBamber
TGC Lead Developer
24
Years of Service
User Offline
Joined: 21st Jan 2000
Location: England
Posted: 6th Jan 2012 11:20
I've added SetVirtualButtonImageDisabled to the suggestions list for 107.

I drink tea, and in my spare time I write software.
kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 28th Jan 2012 17:39
thank you Rich Dersheimer ive used your code with some of my own changes it now works thanks

kirtnicholls
The Slayer
Forum Vice President
15
Years of Service
User Offline
Joined: 9th Nov 2009
Playing: (Hide and) Seek and Destroy on my guitar!
Posted: 28th Jan 2012 21:53
@LeeBamber:
It would be nice if you could add a command that sets the width/height of the virtualbutton aswell. Thanks.

blueFire
13
Years of Service
User Offline
Joined: 23rd Jan 2011
Location: North Carolina
Posted: 29th Jan 2012 01:01
Also being able to set the size of the text (both width (X) and height (Y) independently) inside the button would be great.

I also agree that being able to set the width (X) and height (Y) independently of each other (instead of using one size setting) would be great.

Jason
kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 29th Jan 2012 13:52
ive added it and it ALL WORKS APPART FOR THE WHITE SQUARE AND THE FACT I CANT GET THE IMAGE TO COME BACK... IS THAT MEANT TO HAPPEN OR HAVE I MESSED IT UP SOMWHERE :L

kirtnicholls
kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 4th Feb 2012 21:57
is this meant to happen?

kirtnicholls
kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 4th Feb 2012 21:58
the picture disapears and a white square replaces it but it still works

kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 4th Feb 2012 22:12
Why don't you post some code and maybe we can help?

kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 5th Feb 2012 15:11


kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 5th Feb 2012 16:56
Phew! Ok I'll take a look when I get a chance...

kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 5th Feb 2012 17:20
the end bit is the button, thank , im guessing you didnt expect that amount of coding

kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 09:53
I think it's this part:


You are loading an image called "back" and assigning the image to a sprite called "sprite". Then in the loop you are trying to affect a sprite called "back" (which doesn't exist).

Try changing it to this:

I hope that helps!

kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 10:49
it doesnt work... a white box takes its place :S

kirtnicholls
kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 10:49
for both buttons

kirtnicholls
kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 11:45
ive fixed it


setdisplayaspect (0.66)
SetDisplayAspect ( 4.0 / 3.0 )
setclearcolor (250,250,250)

cardback =loadimage ("cardback.png")
sprite = createsprite (cardback)
setspritesize (cardback,12,21)

do
if GetPointerState()
x# = GetPointerX()
y# = GetPointerY()

if GetSpriteHit(x#, y#) = cardback
SetSpritesize (cardback,12,21)
endif
endif

if GetPointerReleased()
x# = GetPointerX()
y# = GetPointerY()

if GetSpriteHit(x#, y#) = cardback

endif
endif
sync()
loop

kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 12:01
No you haven't...

You are referring to a sprite called "cardback" which does not exist. You have loaded an image called "cardback" not a sprite called "cardback". It is fundamental that you understand this.

It is likely that the image and sprite have the same ID value because you are creating them automatically but if you happen to create more sprites before loading this image your code will stop working and you won't know why.



kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 12:28
yeah i have cause it works, ive run the code and its fixed

kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 12:28
OK then, you know best.

kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 12:42
ill show you, i know youve been coding for ages but it does work. im not trying to be rude an dif i have offended you im sorry i didnt mean to i just dont see why you say its wrong.

kirtnicholls

Attachments

Login to view attachments
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 12:54
I'm not offended I just didn't see the point in trying to help if you didn't want to listen. No offence back, honestly

Wow, powerpoint presentation really?

There were two problems in the original code:
First is you were looking for a sprite called "cardback" which doesn't exist. It is pure coincidence that the sprite and image have the same ID. Your code may work without getting this right but if you don't understand that it is wrong you will continue coding incorrectly in the future and cause yourself more grief than you can imagine.

Second you were changing the image for the sprite to "cardbackImg" which has not been loaded anywhere in the code (hence the white sprite).

The important thing is that you understand what I wrote.

1-Load an image and assign it to an "imageID"
2-Create a sprite using the "imageID" and assign it to a "spriteID"
3-Use the spriteID when affecting the sprite in any way

Example:
imageID = loadImage(imageName$)
spriteID = createSprite(imageID)
setSpritePosition(spriteID,x#,y#)

kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 12:57
so you mean ive been making sprite but not using them in a way? ive been using images and not the sprites?

kirtnicholls
kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 13:01
dont worry i dont take offence, it would be stupid if i did because i would never learn anything

kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 13:05
Not quite.

What happens when you load an image and let AppGameKit decide what imageID to give it is it starts at 10,000 and increments that number for each auto-generated imageID after that. It does the same for spriteID's (starting at 10,000) so since you currently only created one image and one sprite they both have an ID of 10000. If you print the value of "sprite" and "cardback" you'll see they are both "10000".

If you continue this way you will find errors creeping in, hard to trace errors.

kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 13:11
so theres conflict between them meaning if i try make a piece of coding it may not work because it doesnt know which one to choose?

kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 13:16
When you make a larger game it is good practice to load your media before you start using it so what will happen is you will load 30 images (for example) and later create sprites using those images. What will happen if you don't use the image number by mistake is you'll either call for a sprite that doesn't exist or you'll call the wrong sprite.

Just follow my guidelines and this will never happen:
imageID = loadImage(imageName$)
spriteID = createSprite(imageID)
setSpritePosition(spriteID,x#,y#)
...do anything at all to a sprite using spriteID.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 13:27
Sorry for the double post:
Can I suggest you take a look at the first two "AGK Bitesize" tutorials that I have recently started writing for the TGC newsletter? They will soon have their own home on the AppGameKit website but for now you can see them on the App Developers Group website here:
AGK Bitesize 1
AGK Bitesize 2
I'm trying to build up a simple-ish introduction to AppGameKit for users of all abilities and I'd value your opinion on how useful they are. Hopefully they will help you to understand some of the concepts behind AppGameKit a little better

kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 13:28
so is this right

circle1= loadimage ("circle1.png")
sprite1=createsprite (circle1)
setspritesize (sprite1,12,21)

circle2= loadimage ("circle2.png")
sprite2=createsprite (circle2)
setspritesize (sprite2,12,21)

kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 13:30
Yes! You have it now

kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 13:34
i have 75 sprite and id like to make a rondom sprite appear in a set place how can i do thayt please

kirtnicholls
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 6th Feb 2012 13:57 Edited at: 6th Feb 2012 14:13
This'd make all your 75 sprites be set in randomized positions, across the whole screen.

BASIC code:



C++ code:



baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 14:06
I haven't seen that version of the random command in Tier 1 kamac, is that right?

I would normally do it like this:


Also you would have to set the spriteID's to match by creating your sprites like this:


kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 6th Feb 2012 14:12 Edited at: 6th Feb 2012 14:14
baxslash, Woops.

@EDIT.

Fixed.

kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 14:19
all 75 are different pictures, but if its not possible to do it dont worry thanks anyway

kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 14:31
I just re-read your post, you want to set a random sprite in a set place right?

Just put all your sprite ID's in a simple array like this:


Then to select a random sprite to put into position do this:


kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 14:53
could i use that code in a way to set all thier sizes the same but with 1 line of coding?

kirtnicholls
kirtn14
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location:
Posted: 6th Feb 2012 14:54
would it be

?

kirtnicholls
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Feb 2012 14:55 Edited at: 6th Feb 2012 14:57
Yes:


In answer to the post you just beat me with, no. use something like above.

Another thing, edit your previous post using the "Edit Post" link below it unless someone has posted since your last message. "Double posting" occasionally is OK but the moderators will not like it if you do it a lot like you are doing.

Login to post a reply

Server time is: 2024-11-23 01:30:24
Your offset time is: 2024-11-23 01:30:24