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.

Windows / Why doesn't my function work?

Author
Message
plixr
14
Years of Service
User Offline
Joined: 3rd Sep 2004
Location:
Posted: 30th Jun 2014 22:37
I am relatively new to programming, and thought i was doing good until i tried to create a function and now i am getting an error. I feel like it may have something to do with variable scope, but I am not totally sure. I am sure a seasoned coder will be able to spot my mistake quickly so I am asking for a little help on why i am getting a "sprite does not exist" error.


P43GHz,1GBRAM,Geforce 7300gs,WindozeXPhomesp2
Lucas Tiridath
AGK Developer
10
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 11th Jul 2014 10:47 Edited at: 11th Jul 2014 10:48
The problem is to do with the way you are initialising your globals. For whatever reason (I don't know if this is a bug or a deliberate part of the semantics, or whether it will change in V2), you can only initialise your globals with literal values. This means that your initialisation of sGlobalWelcome$ works, because you are initialising with a string literal, but your initialisation of iball and ball fail because you are calling functions which will be executed at runtime. This means that at the moment, your LoadImage and CreateSprite calls are completely ignored, and iball and ball are default initialised to 0, meaning that when you try to use them in the SetSpritePosition function, you get a sprite 0 does not exist error. Therefore if you change the code to the following, it should work for you.



Also I think it's worth pointing out that you don't need semi-colons in AppGameKit BASIC. I hope that helps!

EDIT: Woah sorry, just noticed how late this response is. Yeah generally it's best to post in the main AppGameKit board as it is more active than this one.

Ancient Lady
Valued Member
15
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd Jul 2014 21:51
I may be wrong (currently having a sick day with nasty cold and foggy head), but don't you need to declare the globals in the function?


Cheers,
Ancient Lady
Van B
Moderator
16
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 23rd Jul 2014 17:28
I think it's best to declare those as globals outside of the function.

So some rules for the OP:

Rule(1)
When declaring globals, use exact values, not system returned values which just won't work when declaring.

So instead of...
global iball = loadimage("crappyBall.png");
global ball = createSprite(iball);

Have...
global iball ball
iball = loadimage("crappyBall.png");
ball = createSprite(iball);

Rule(2)
Any variables that you need to affect inside a function, needs to be a global, or an array. So that Moveball function is using several variables which will all equal 0, because it is not keeping track due to those variables not being global - so globalize them as AL suggests, maybe be neater to make a type for them though... like this:

Type _entity
ID
x# y# xs# ys# dx# dy#
height width
Endtype

Then you can make your ball entity...

Global Ball as _entity

Then the type can be accessed with Ball.ID, Ball.x# etc etc - this can help keep code neat and well organized. If you globalize before the function, then you will be able to affect the variables as expected, like 'height as integer' would be 'Global height as integer' - then height could be used inside a function. When you need to have a lot of enemies, or a lot of bullets or coins to collect, then it's always best to use an array, which can use the same type as Ball. I advise looking into these type arrays before doing much else with your project. I think it makes best sense to work like this:

Dim Coin[64] as _entity
for c=0 to 63
Coin[c].ID=createsprite(img)
Coin[c].x#=random(0,512)
Coin[c].y#=random(0,512)
next c

That sorta thing.

I am the one who knocks...

Login to post a reply

Server time is: 2019-03-23 16:25:40
Your offset time is: 2019-03-23 16:25:40