Compiled Error in Xcode for iphone game and other questions

Dear all,
Hi, I am a newbie of xcode and objective-c and I have a few questions regarding to the code sample of a game attached below. It is written in objective C, Xcode for iphone4 simulator. It is part of the code of 'ball bounce against brick" game. Instead of creating the image by IB, the code supposes to create (programmatically) 5 X 4 bricks using 4 different kinds of bricks pictures (bricktype1.png...). I have the bricks defined in .h file properly and method written in .m.
My questions are for the following code:
- (void)initializeBricks
brickTypes[0] = @"bricktype1.png";
brickTypes[1] = @"bricktype2.png";
brickTypes[2] = @"bricktype3.png";
brickTypes[3] = @"bricktype4.png";
int count = 0;`
for (int y = 0; y < BRICKS_HEIGHT; y++)
for (int x = 0; x < BRICKS_WIDTH; x++)
- Line1 UIImage *image = [ImageCache loadImage:brickTypes[count++ % 4]];
- Line2 bricks[x][y] = [[[UIImageView alloc] initWithImage:image] autorelease];
- Line3 CGRect newFrame = bricks[x][y].frame;
- Line4 newFrame.origin = CGPointMake(x * 64, (y * 40) + 50);
- Line5 bricks[x][y].frame = newFrame;
- Line6 [self.view addSubview:bricks[x][y]]
1) When it is compiled, error "ImageCache undeclared" in Line 1. But I have already added the png to the project. What is the problem and how to fix it? (If possible, please suggest code and explain what it does and where to put it.)
2) How does the following in Line 1 work? Does it assign the element (name of .png) of brickType to image?
brickTypes[count ++ % 4]
For instance, returns one of the file name bricktype1.png to the image object? If true, what is the max value of "count", ends at 5? (as X increments 5 times for each Y). But then "count" will exceed the max 'index value' of brickTypes which is 3!
3) In Line2, does the image object which is being allocated has a name and linked with the .png already at this line *before* it is assigned to brick[x][y]?
4) What do Line3 and Line5 do? Why newFrame on left in line3 but appears on right in Line5?
5) What does Line 4 do?
Thanks
North

Hi North -
macbie wrote:
1) When it is compiled, error "ImageCache undeclared" in Line 1. ...
UIImage *image = [ImageCache loadImage:brickTypes[count++ % 4]]; // Line 1
The compiler is telling you it doesn't know what ImageCache refers to. Is ImageCache the name of a custom class? In that case you may have omitted #import "ImageCache.h". Else if ImageCache refers to an instance of some class, where is that declaration made? I can't tell you how to code the missing piece(s) because I can't guess the answers to these questions.
Btw, if the png file images were already the correct size, it looks like you could substitute this for Line 1:
UIImage *image = [UIImage imageNamed:brickTypes[count++ % 4]]; // Line 1
2) How does the following in Line 1 work? Does it assign the element (name of .png) of brickType to image?
brickTypes[count ++ % 4]
Though you don't show the declaration of brickTypes, it appears to be a "C" array of NSString object pointers. Thus brickTypes[0] is the first string, and brickTypes[3] is the last string.
The expression (count++ % 4) does two things. Firstly, the trailing ++ operator means the variable 'count' will be incremented as soon as the current expression is evaluated. Thus 'count' is zero (its initial value) the first time through the inner loop, its value is one the second time, and two the third time. The following two code blocks do exactly the same thing::
int index = 0;
NSString *filename = brickTypes[index++];
int index = 0;
NSString *filename = brickTypes[index];
index = index + 1;
The percent sign is the "modulus operator" so x%4 means "x modulo 4", which evaluates to the remainder after x is divided by 4. If x starts at 0, and is incremented once everytime through a loop, we'll get the following sequence of values for x%4: 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, ...
So repeated evaluation of (brickTypes[count++ % 4]) produces the sequence: @"bricktype1.png", @"bricktype2.png", @"bricktype3.png", @"bricktype4.png", @"bricktype1.png", @"bricktype2.png", @"bricktype3.png", @"bricktype4.png", @"bricktype1.png", @"bricktype2.png", ...
3) In Line2, does the image object which is being allocated has a name and linked with the .png already at this line *before* it is assigned to brick[x][y]?
Line 2 allocs an object of type UIImageView and specifies the data at 'image' as the picture to be displayed by the new object. Since we immediately assign the address of the new UIImageView object to an element of the 'bricks' array, that address isn't stored in any named variable.
The new UIImageView object is not associated with the name of the png file from which its picture originated. In fact the UIImage object which inited the UIImageView object is also not associated with that png filename. In other words, once a UIImage object is initialized from the contents of an image file, it's not possible to obtain the name of that file from the UIImage object. Note when you add a png media object to a UIImageView object in IB, the filename of the png resource will be retained and used to identify the image view object. But AFAIK, unless you explicitly save it somewhere in your code, that filename will not be available at run time.
4) What do Line3 and Line5 do? Why newFrame on left in line3 but appears on right in Line5?
5) What does Line 4 do?
In Line 2 we've set the current element of 'bricks' to the address of a new UIImageView object which will display one of the 4 brick types. By default, the frame of a UIImageView object is set to the size of the image which initialized it. So after Line 2, we know that frame.size for the current array element is correct (assuming the image size of the original png file was what we want to display, or assuming that the purpose of [ImageCache loadImage:...] is to adjust the png size).
Then in Line 3, we set the rectangle named newFrame to the frame of the current array element, i.e. to the frame of the UIImageView object whose address is stored in the current array element. So now we have a rectangle whose size (width, height) is correct for the image to be displayed. But where will this rectangle be placed on the superview? The placement of this rectangle is determined by its origin.
Line 4 computes the origin we want. Now we have a rectangle with both the correct size and the correct origin.
Line 5 sets the frame of the new UIImageView object to the rectangle we constructed in Lines 3 and 4. When that object is then added to the superview in Line 6, it will display an image of the correct size at the correct position.
- Ray

Similar Messages

  • Best MMORPG for Iphone 3G and a question about gps maps

    Hi there,
    Could anyone tell me what are the best free and paid iphone 3G MMORPG games? And please tell me some positive and negative things in the games.
    I didn't know where i could ask this so, i came here to apple forum, because i have subsribed to internet on iphone today. This topic doesn't have anything illegal.
    Another thing, does anyone knows if using gps maps through 3g internet, spends a lot of internet trafic? today i've used gps maps for about 12km and it spend me 80kb of trafic. Is this possible? because someone told me that iphone gps spend a lot, but really lot internet trafic. I didn't used sattelite view.

    The sattelite view uses far more data, of course - because of the photography it has to download. But the regular map is still a bitmapped image that has to be updated. It's not a massive amount of data, but not insignificant either.

  • I dont get sound for videos, games and music but ringtone still works. When i press volume buttons on side of phone i only get the picture of the nell and no little dots underneath. This is on my iphone 4s and ipad. It happened at the same time.

    I dont get sound for videos, games and music but ringtone still works. When i press volume buttons on side of phone i only get the picture of the nell and no little dots underneath. This is on my iphone 4s and ipad. It happened at the same time.

    Recurring theme with the 4s losing alerts except for the phone ringing.  Most complaints seem to center on the 7.1 upgrade.  The fix is dodgy, putting in a charger, playing a you tube video then pulling out the charger and wiggling it until you get the sound back.  When you get the sound back, the volume buttons start to work again.  Alternate solutions involve cleaning the docking port with a toothbrush, running a pick over the electrodes etc.

  • Looking for iphone games for mental math or vedic math training?  Dear all,

    Looking for iphone games for mental math or vedic math training?
    Dear all,
    I am a fan of mental math and/or vedic math etc. and I want to utilize my spare time to do some training whenever I am idle.
    That's why I want to buy an iphone. In fact, I buy iphone because it can be a game machine in addition to a phone.
    For example, I can utilize my time commuting on subway to do some training...
    But I couldn't find good mental math and/or vedic math training game on iphone.
    I am an adult so those too-simple problems such as 2*9=18 are not interesting to me,
    I would like to exercise something like 123x45, etc.
    Could anybody give me some pointers?
    Thanks!

    need quick math training games,... thanks!

  • HT3702 Im am trying to buy coins for my games and it says purchase cant be completed, credit card details are correct, is there a limit??

    Im am trying to buy coins for my games and it says purchase cant be completed, credit card details

    What exactly does the error say?

  • BUG: 10.1.3..36.73 Internal Compile Error with enhanced for loop/generics

    I get the following compiler error when using the Java 5 SE enhanced for loop with a generic collection.
    Code:
    public static void main(String[] args)
    List<Integer> l = new ArrayList<Integer>();
    l.add(new Integer(1));
    printCollection(l);
    private static void printCollection(Collection<?> c)
    for (Object e : c)
    System.out.println(e);
    Error on attempting to build:
    "Error: Internal compilation error, terminated with a fatal exception"
    And the following from ojcInternalError.log:
    java.lang.NullPointerException
         at oracle.ojc.compiler.EnhancedForStatement.resolveAndCheck(Statement.java:2204)
         at oracle.ojc.compiler.StatementList.resolveAndCheck(Statement.java:4476)
         at oracle.ojc.compiler.MethodSymbol.resolveMethod(Symbol.java:10822)
         at oracle.ojc.compiler.RawClassSymbol.resolveMethodBodies(Symbol.java:6648)
         at oracle.ojc.compiler.Parser.resolveMethodBodies(Parser.java:8316)
         at oracle.ojc.compiler.Parser.parse(Parser.java:7823)
         at oracle.ojc.compiler.Compiler.main_internal(Compiler.java:978)
         at oracle.ojc.compiler.Compiler.main(Compiler.java:745)
         at oracle.jdeveloper.compiler.Ojc.translate(Ojc.java:1486)
         at oracle.jdeveloper.compiler.UnifiedBuildSystem$CompileThread.buildGraph(UnifiedBuildSystem.java:300)
         at oracle.jdeveloper.compiler.UnifiedBuildSystem$CompileThread.buildProjectFiles(UnifiedBuildSystem.java:515)
         at oracle.jdeveloper.compiler.UnifiedBuildSystem$CompileThread.buildAll(UnifiedBuildSystem.java:715)
         at oracle.jdeveloper.compiler.UnifiedBuildSystem$CompileThread.run(UnifiedBuildSystem.java:893)

    Install the Service Update 1 patch for JDeveloper (using the help->check for updates), and let us know if this didn't solve the problem.

  • When will we beable to download apps to the iphone games and better feature

    when will we beable to download apps to the iphone games and better features
    karl
    i hear something about 1.1.4 coming out next couple weeks that true ???

    The release of the SDK is scheduled sometime this month. That will allow developers to design software for the phones. After approval from Apple, they would be made available, probably through iTunes.
    No one here would know about a release of 1.1.4, or if the next update will even be called 1.1.4. That is up to Apple and they don't release that information early.
    Hope this helps you.

  • I have two iphone one is iphone 5 and othere is iphone 4 . can i use same id for both ? And do i have to purchase the application again for iphone 4 which i have buyed for iphone 5.??

    i have two iphone one is iphone 5 and othere is iphone 4 . can i use same id for both ? And do i have to purchase the application again for iphone 4 which i have buyed for iphone 5.??

    You can use the same Apple ID for both phones, you would then be able to use the purchased Apps/Content on both phones without having to pay again.

  • I have an iphone 4s and have installed the new ios download and can no longer find games and other apps that i have downloaded. Where would they be or have they been wiped?

    I have an iphone 4s and have installed the new ios download and can no longer find games and other apps that i have downloaded. Where would they be or have they been wiped?

    Hi,
    don´t be worry, this would not be a problem.
    You have to take the same Aplle ID on both devices, then you can sync them via iTunes by using all universal apps on each device, create playlists, albums and all the things you need and sync it, no problem...
    cu
    dienoppe

  • HT1414 Why every time i download games and other category in app store it always says " an unknown error has occured" cant sign in what will i do please help me

    Why everytime i download games and other category in app store i always receive a mesaage that it says " An unknown error has occured" cant sign in what will i do

    Only Apple can handle this:
    iTunes Support

  • What is the latest iOS for iPhone 3GS and will there be another update

    What is the latest iOS for iPhone 3GS and will there be another update or is it iOS 6.1.3 and no more

    The 3GS is not listed in the supported list for iOS7. Unless Apple comes out with a major bug fix, 6.1.3 will likely be the last update for it.

  • Hi,  I would like to ask you a question. a month ago I bought a battery case for iPhone 5 and it worked properly until I installed iOS 7. From that day iphone is operating normally, but when the battery is empty, unfortunately I can not charge additionall

    Hi,
    I would like to ask you a question.
    a month ago I bought a battery case for iPhone 5 and it worked properly until I installed iOS 7. From that day my iphone is operating normally, BUT when the battery is empty, unfortunately I can not charge additionally my iphone with the "extra battery case".
    Does that mean that you did not even create a new software that supports additional battery case or it is something else?
    I need your help!!,
    Thank you in advance.
    Sincerely,
    VK

    The Mophie Air is a good battery case, but doesn't offer the protection she's looking for. I suggest sticking to the Lifeproof, the iPhone has a very good battery (2-3 days moderate usage for me).
    To your 2nd to last paragraph, that is false. Verizon is allowing exisiting customers to keep Unlimited Data - but will lose it once they use a subsidized upgrade. So if you buy a 4S (or LTE device for example) outright, you will keep unlimited data. Mobile hotspot may use up a bit of data, but it is a separate add-on/feature. The new Family Share plans may change that, so it's one giant center of data all devices use.

  • I want to add a solar charger for iPhone 5 and iPad to my emergency kit. Are there recommendations for compatible solar products?

    I want to add a solar charger for iPhone 5 and iPad to my emergency kit. Are there recommendations for compatible solar products?

    If you are seldom in a Wi-Fi Network Area, then you can turn off Wi-Fi until you knowingly are in such a Network Area.  You can go through your Applications where you don't really need to know the instant an Update is available and turn off the push update notifications.  You can turn down the screen brightness.  You can set the sleep cycle to 1-min.  You could set your email to check for new messages every 30-min. rather than less than 1 min.  Just some of many steps available.

  • I've bought headphones for iphone 4g and want to use them in my ipod 4g. When i want to use pilot it don"t work. What should i do ?

    I've bought headphones for iphone 4g and want to use them in my ipod 4g. When i want to use pilot it don"t work. What should i do ?

    If you have an Apple store nearby you can make an appointment at their Genius Bar.
    Apple Retail Store - Genius Bar
    You can also try:
    - Reset the iOS device. Nothing will be lost
    Reset iOS device: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Reset all settings
    Go to Settings > General > Reset and tap Reset All Settings.
    All your preferences and settings are reset. Information (such as contacts and calendars) and media (such as songs and videos) aren’t affected.
    - Restore from backup. See:
    iOS: How to back up
    - Restore to factory settings/new iOS device.
    iOS: How to back up your data and set up as a new device

  • Genuine Leather case for iPhone 6 and iPhone 6 plus

    Hi, I'm looking for premium quality leather case for iPhone 6 and iPhone 6 plus. I don't want PU but genuine quality. Still would like not too expensive. All that I found so far are around hundred bucks. Could someone advice of brands for that category? Or personal experience ... Thanks

    Have a look at cableJive they make the dockStubz which might do what you are looking for

Maybe you are looking for

  • Drop down list in Table Control - Value disappeared after pressing ENTER

    Dear Gurus, I have a problem in one of my development where I used a table control in which one field is a drop down list. I used VRM_SET_VALUES and VRM_GET_VALUES for populating the values from the programand not use any domain level value table for

  • Source Account Validation Report

    Goodmorning, I'm building source validation rules (without problem) but a boring problem occurs: Scenario: Location -- E2 (source entity:00002) |__ E6 (source entity:00006) |__ E7 (source entity:00007) Validation report result: E2 Rule 1 amount 123 R

  • How do i stop the emails "eprint job received"

    how do i stop the emails "eprint job received"?

  • Account Balance Zeroed Out 2013-12-21

    My wife's prepaid wireless account balance of $293+ was zeroed out today for some reason. Calling either of the customer service number provided only results in prompts to add money to the account, and of course, nobody uses their phone on the weeken

  • Error registering crqe.dll

    Post Author: wkiess CA Forum: General Hi All, I am working with VB6 SP6 and CR10 and have used the VB6 Package and Deployment Wizard to create a setup for my application. When running the setup in a WinXP VM, I got an error message towards the end of