Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 11, 2011, 11:03 AM   #1
xcodeNewbie
macrumors member
 
Join Date: Jul 2011
Help Creating a UIImageView

I'm making a game in which the user controls a ship and has to shoot at other ships that fly by. When the user presses the button that says "shoot", the method "shoot" gets called, creating a UIImageView for the shot.
Code:
-(IBAction)shoot {
    UIImageView *shot = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shot.jpg"]];
    [shot setBounds:CGRectMake(0, 0, 30, 30)];
}
I set the image to the top right corner of the screen for testing purposes. However, when I run the program and press "shoot", nothing happens. What am I doing wrong?
xcodeNewbie is offline   0 Reply With Quote
Old Jul 11, 2011, 11:07 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
You have not added the view as a subview of an existing visible view. So it is never drawn as it is not part of the on-screen view hierarchy.
robbieduncan is offline   0 Reply With Quote
Old Jul 11, 2011, 11:17 AM   #3
pulsewidth947
macrumors 65816
 
pulsewidth947's Avatar
 
Join Date: Jan 2005
Location: squarefrog.co.uk
Code:
-(IBAction)shoot {
    UIImageView *shot = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shot.jpg"]];
    [shot setBounds:CGRectMake(0, 0, 30, 30)];
    [window addSubview:shot]; // syntax may be wrong there.. but along the right lines
    [shot release] // dont forget - if you alloc, you are responsible for releasing.
}

Also - if you haven't already seen it, start lurking on stack overflow.
__________________
Refurb 15" Glossy MBP: 2.4GHz SR, 120GB OCZ Vertex 2, 320GB HD, 4GB RAM.
Mac Mini: 1.83GHz Core Duo, 2GB RAM.
pulsewidth947 is offline   0 Reply With Quote
Old Jul 11, 2011, 11:25 AM   #4
xcodeNewbie
Thread Starter
macrumors member
 
Join Date: Jul 2011
Another UIImageView question

This is sort of an add on to a previous question...I create a UIImageView programmatically in the code below.
Code:
-(IBAction)shoot {
    UIImageView *shot = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shot.jpg"]];
    [shot setBounds:CGRectMake(ship.center.x-15,ship.center.y-15,25,25)];
    [superView addSubview:shot];
    [shot release];    
}
I'm trying to make it so the shot appears right above my ship, so it appears the ship is shooting. However, no matter how I change the CGRetMake, the shot will always appear in the upper left part of the screen. I even once set the starting coordinates to 99999999999 just to see what would happen. It still appeared in the top left corner of the screen.
xcodeNewbie is offline   0 Reply With Quote
Old Jul 11, 2011, 11:33 AM   #5
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Read, and understand, the View Programming Guide. In particular pay attention to the difference between a views frame and it's bounds.
robbieduncan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
UIImageView Transition alaahershy iPhone/iPad Programming 5 Sep 10, 2013 04:28 PM
Setting UIImageView Behind UITextView RagingGoat iPhone/iPad Programming 4 May 19, 2013 07:11 PM
UIImageView problem flashyx iPhone/iPad Programming 1 Mar 2, 2013 04:10 AM
Reordering UIImageView layers xArtx iPhone/iPad Programming 9 Jan 16, 2013 06:21 AM

Forum Jump

All times are GMT -5. The time now is 11:10 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC