Learning ASS and xCode by making a little memory card game, i have to “load image” at start. And ther is a lot of image. So I would like to make a loop. Here i have :
at top of the as :
property TabDefaut : {} -- which is used to stock the variable ImgX for later...
property Img1 : ""
property Img2 : ""
property Img3 : ""
-- ... x27
and the image source are named like this :
“img1” , “img2”, “img3” …
So, how can i do a loop for loading image named (“img”&n) in a variable named (“Img”&n) ?
i try this (but not working, of course) :
on awake from nib theObject
repeat with n from 1 to 27
set ("Img"&n as variable) to load image ("img"&n) -- give error
set the end of TabDefaut to ("Img"&n as variable)
end repeat
end awake from nib
i try this too (but not working too, of course) :
on awake from nib theObject
repeat with n from 1 to 27
set bob to ("Img"&n as variable)
set bob to load image ("img"&n) -- stupid because the image is loading in bob not in ImgX
set the end of TabDefaut to ("Img"&n as variable)
end repeat
end awake from nib
Well then, is there a way to determine dynamicly the named of the variable to use ?
Thank to you…
A+R.
Browser: Safari 523.12
Operating System: Mac OS X (10.4)
Not possible in ASStudio. You could set up a huge record list containing all of your image references and load them all at launch time, but they’ll still need to be hard-coded. You might be better of just loading images as they are necessary.
One thing to remember, is that cocoa controls retain their images themselves, so you don’t have to keep a local copy around. The key to doing this efficiently is to work out a solid data model, from which you can derive the data you need to determine which image to display. For example, in the code example below you would start by changing your data model. In your context, this would involve setting the data that tells a particular card what image pair it belongs to or whether it’s flipped over or not. Then you might write a subroutine that would poll your data model for the name of the image file to display for it’s current state. Then, you would load that image. The next step is one that many ASStudio developers don’t really grasp at first (or ever). When you issue a “set image of someObject…” command, that object copies the image and keeps that copy for itself. The copy you have in your code (stored in the ‘theImage’ variable) can… and SHOULD… be disposed of immediately after setting the image of the object. Otherwise, every time you load another image (which could happen LOTS of times in the course of playing a game) you’re making another copy of the image in your memory space. Since the image view or button retains it’s own copy, just set it and then delete the instance you created. That’s where the delete command comes into play, in the last line. Note that I used an image view in this example, but buttons work the same way.
--> Set your data model state here
set theImageName to deriveAnImageNameBasedOnSomeCriteria()
set theImage to (load image theImageName)
set image of image view "theImageView" of window "theWindow" to theImage
delete theImage
to deriveAnImageNameBasedOnSomeCriteria()
--> Do some processing here to figure out what image to return
return "Picture 2.png"
end deriveAnImageNameBasedOnSomeCriteria