How do I determine positions of an object?

Discussion in 'iOS Programming' started by BrandonStore, Nov 15, 2010.

  1. BrandonStore, Nov 15, 2010
    Last edited by a moderator: Nov 15, 2010

    BrandonStore macrumors newbie

    Joined:
    Nov 15, 2010
    #1
    Okay so I am working on a little ipad game for school. My problem is, I am trying to make this if statement work...



    Code:
    -(IBAction)CheckPosition{ 
    	
    	if (Object1.x && Object1.y == Object2.x && Object2.y){
    		
    		UIAlertView *playerWin = [[UIAlertView alloc] initWithTitle:@"You Win" message:@"GOOD JOB!" delegate:nil cancelButtonTitle:@"OKAY!" otherButtonTitles:nil];
    		[playerWin show];
    	
    	
    	}
    }
    
    error: request for member 'x' in something not a structure or union


    however it does not work. I figured I am going about it the wrong way, pretty much what I want to do is if object1 is directly over object2 the win message appears. My objects have outlets and are linked to my interface builder objects. Any advice, answers, anything will be really appreciated. I am a novice programmer and Objective-C is my first language.

    Thanks,

    Brandon
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    First, your code uses Object1 and Object2 but your description mentions object1 and object2. Remember, Objective-C is case-sensitive.

    Also, you have not shown us how these objects are defined and set.

    Second, you need to rethink the logic and operator precedence of your if condition.
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    If think what you meant was ...

    Code:
    	if ((Object1.x == Object2.x) && (Object1.y == Object2.y)){
    
     
  4. BrandonStore, Nov 15, 2010
    Last edited by a moderator: Nov 26, 2010

    BrandonStore thread starter macrumors newbie

    Joined:
    Nov 15, 2010
    #4
    Thanks for the responses, llyoddean I tried what you suggested and it gave me a new error

    ERROR: Request for member 'x' in something not a structure or union

    Dejo, Yeahh I am aware of the case sensitive, that was a typo (but that was defiantly a good pointer). this is how I defined the objects, this is under the .h file .....

    Code:
    @interface MyGameViewController : UIViewController { 
    
    	IBOutlet UIImageView *Object1; 
    	IBOutlet UIImageView *Object2;
    	
    }
    
    
    	 
    @property (nonatomic, retain) 	IBOutlet UIImageView *Object1; 
    @property (nonatomic, retain)	IBOutlet UIImageView *Object2;
    
    @end 

    So anyway, what I thought would work would be a simple if statement that asks, if their position to one another is exactly the same. Am I doing it completely wrong? Should I be going about the whole process differently? Thanks again, I appreciate it.


    EDIT: Oh and I linked the objects in interface builder with Object1 and Object2
     
  5. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    Previous posts didn't give a clue as to what Object1 and Object2 were. Now that we now know that they are pointers to UIImageView's we know that UIImageViews have no fields called x and y thus your error.

    You really need to provide more context as to what you're trying to accomplish?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    First, the common convention is to name instance variables starting with lower-case letters so as to not confuse them with class names, which normally start with an upper-case letter.

    Second, what makes you think that UIImageView has x and y properties? Just trying to understand where you've been taught that.
     
  7. BrandonStore thread starter macrumors newbie

    Joined:
    Nov 15, 2010
    #7
    ahh well to be honest, I just figured it would have x and y properties. I figured a UIImageView is an object... I guess I am wrong? So basically I guess I need to make them objects and then the code suggested would work?
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    No need to "figure". The class reference is readily available, which documents the properties of the class as well as links to all the superclasses, so you can see their properties as well. Guessing about this kind of stuff is a very bad habit and only leads to further problems; don't do it.

    No, not wrong exactly. UIImageView is a class and your Object1 and Object2 are instances of that class, so they are objects.

    See above.

    P.S. There are x and y properties deeper down within your object. You just need to figure out the proper way to reference them.
     
  9. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #9
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
  11. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #11
    Doh :/

    So would you use the frame property's values, or the bound's property? Sounds like they're sort of the same values.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Read this to understand the difference: View Geometry
     
  13. BrandonStore thread starter macrumors newbie

    Joined:
    Nov 15, 2010
    #13
    Hey Dejo, I read the page you linked. I am not sure what to do though. What do you mean by, there are x and y properties deep in my object? Is there a certain way to code my statement for it to work out?
     
  14. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #14
    Yes, there is.

    Properties can have properties can have properties. Just because you have great-grandparents doesn't make you your grandfather's brother.
     
  15. BrandonStore thread starter macrumors newbie

    Joined:
    Nov 15, 2010
    #15
    okay.... nothing is really clicking at all for me. How do I go about defining the X and Y axis for my UIImageView objects?
     
  16. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #16
    You've given us oh so ..., wait, you've given us no context at all as a basis of which to give you a useful answer.

    You really need to describe what you want to do as well as how you're attempting to do it now.
     
  17. BrandonStore, Nov 18, 2010
    Last edited by a moderator: Nov 26, 2010

    BrandonStore thread starter macrumors newbie

    Joined:
    Nov 15, 2010
    #17
    okay, fair enough. This is exactly what I want to do. I want to be able to make a "Win" condition for when my object intersects with another. So far the objects are both UIImageView, this is the code that wrote to initialize them, under the .h file....


    Code:
    @interface LearningBoxViewController : UIViewController { 
    
    	IBOutlet UIImageView *objCircleBlue; 
    	IBOutlet UIImageView *objCircleBlueBracket;
    	
    }
    
    
    	 
    @property (nonatomic, retain) 	IBOutlet UIImageView *objCircleBlue; 
    @property (nonatomic, retain)	IBOutlet UIImageView *objCircleBlueBracket;

    I went into Interface Builder to link the IBOutlets to the UIImageViews. Now I went under the .m file and wrote this,

    Code:
    -(IBAction)CheckPosition{ 
    	
    	if ((objCircleBlue.x == objCircleBlueBracket.x) && (objCircleBlue.y == objCircleBlueBracket.y)){
    		
    		UIAlertView *playerWin = [[UIAlertView alloc] initWithTitle:@"You Win" message:@"Yeahh you won" delegate:nil cancelButtonTitle:@"OKAY!" otherButtonTitles:nil];
    		[playerWin show];
    	
    	
    	}
    }
    now that gives me errors, Basically what I am trying to say is, if objCircleBlue is equal position to objCircleBlueBracket then player Wins.

    The errors that pop up are

    Request for member 'x' in something not a structure or union
    Request for member' y' in something not a structure or union

    and each error pops up twice, I'm assuming its an error for each object position I wrote. So, I am just stuck without a clue on how to write a statement that asks if the positions are exactly the same, player wins.
     
  18. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #18
    I'm assuming you've also got some drawing code you can post to provide further context?
     
  19. chown33, Nov 18, 2010
    Last edited: Nov 18, 2010

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #19
    If you don't find something in a class, don't forget to look in its superclass.

    UIImageView has UIView as its superclass. So look there, and see if there are any properties that might tell you what its location is.

    Simple strategy: visit the class reference doc and Find a word of interest on the page.

    Reference doc for UIView:
    http://developer.apple.com/library/.../UIView/UIView.html#//apple_ref/occ/cl/UIView

    Possibilities for word of interest:
    position
    location
    coordinate

    Once you find some words of interest, look at the class or type they're in. And so on until you find something useful or the trail dead-ends.

    If you're going to write software, you'll have to get a lot better at following sequences of relationships. The most obvious sequence is between a class and its superclasses, all the way up the chain. Another obvious sequence is between a class and its properties, or more generally the things it "has" (UIImageView has a UIImage, it has a frame, etc.). What something "has" is distinct from what something "is" (a UIImageView is a UIView is a UIResponder is a NSObject).

    http://en.wikipedia.org/wiki/Has-a
    http://en.wikipedia.org/wiki/Is-a

    The way you're writing code now, it seems like you think that the coding is the hard part. I suggest that following the logical relationships is the harder part. Not just the relationships between is-a and has-a, but relationships between things like: Are equal Xs and Ys really the only indication that two things have intersected? It seems to me that's only true if the things are a single pixel in size. Any things bigger than that and you have to look for intersecting areas, not just identical points. Imagine it as two pieces of paper: any overlap at all and it's an intersection. So if you're not thinking about the areas or dimensions of the things, then you've already lost an important relationship: things have a size.
     
  20. BrandonStore thread starter macrumors newbie

    Joined:
    Nov 15, 2010
    #20
    Not sure what you mean to be honest.

    Chown33 I've been looking into the links and its helped. I am not quite there yet but have a better understanding now. Thanks
     
  21. wlh99 macrumors 6502

    Joined:
    Feb 7, 2008
    #21
    I think that you have given enough information to answer your question. But as others have pointed out, that code likely doesn't do what you think, and to solve that they need to see more.

    Try this:

    In your code double click on the text "UIImageView" so it is highlighted.

    Right click on the highlighted text and select "Find Selected Text in API Reference"

    On the left side, click the arrow next to properties to expand that list. Note that there are no x or y properties.

    On the top of the page in the right half of the Window, note that UIImageView Inherits from UIView:UIResponder:NSObject. In other words, an UIImageView is a kind of UIView, which is a kind of UIResponder, which is a kind of NSObject.

    Click on UIView, then expand properties. x and y are not there either. But, since you are looking for the position, maybe center would work? Investigate that and click on center.

    You will see that center is a property of type CGPoint.

    Click on CGPoint. You will see that CGPoint is a struct with two CGFloats - x and y.

    The syntax to access those would be:
    Object1.center.x

    As you learn, you should read the API for every object type you ever use. Keep the reference open and follow the links. It also sounds like you might be misunderstanding what an object is and how OOP works. I would suggest a good intro book if you don't have one. Programming in Objective-C by Stephen G Kochan is highly recommend, and you should be able to go through it very quickly.

    Also, as others pointed out there may be other problems in your code if indeed you are trying to detect a collision of two UIImageViews. But we don't know from just the snippet you posted.
     
  22. BrandonStore thread starter macrumors newbie

    Joined:
    Nov 15, 2010
    #22
    thanks for the post wlh99, I still haven't gotten done what I need. I feel like I have it, and then I don't. So I'm ready to just start over, there really isn't anything more that I can show that I haven't showed already. Does any body know of a good tutorial about 2 UIViews colliding? I've tried searching the internet and I can't find anything on it.
     
  23. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #23
    You keep posting what you think you need to do.

    Not what you're trying to accomplish over all.

    Not the code (or project even) that you're having problems with.

    We don't know what you know; and only some of what you don't!

    Before you start over post everything you're having problems with. Suggestion can be made. And then starting over may be more beneficial to you!
     
  24. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #24
    Heck, post a screen shot if you feel like it. Your problem may not be as complicated as you think it is, and someone may be able to tell what your overall problem is from that.
     
  25. wlh99, Nov 23, 2010
    Last edited: Nov 23, 2010

    wlh99 macrumors 6502

    Joined:
    Feb 7, 2008
    #25
    A good example is the first project in Beginning iPhone Game Development published by Apress. I strongly suggest a printed book over an online tutorial. Not that online won't work, but the ones I have seen aren't as good as a good book. YMMV

    Also consider a beginning book on Objective-C or C before diving into the iPhone. Learning the language and the iPhone APIs at the same time is tough work. But they could be done relatively quickly by an ambitious astute student if taken one at a time.


    Take this line and explain in precise detail what it does(assuming that your previous error was corrected and you now have the x and y positions). Plug in some hypothetical numbers and work it out by hand.

    Code:
    if (Object1.center.x && Object1.center.y == Object2.center.x && Object2.center.y){
    
    What does this evaluate to:
    Code:
    (10 && 20 == 10 && 20)
    
    or this:
    Code:
    (10 && 10 == 20 && 20)
    
    lastly:
    Code:
    (20 && 10 == 10 && 20)
    
    As a learning exercise you might do something like:
    Code:
    if (20 && 10 == 10 && 20) {
       NSLOG(@"True");
    } else {
       NSLOG(@"False");
    }
    
    Be sure you understand what the && operator does, and the order of operations performed on the statement.

    Once you have that figured out, you need to consider that an image has a size. To detect a collision isn't just detecting the x and y locations are the same, but that they are close to the same by whatever size the images are. So you will need a series of if statements, and use the <= and >= operators to check that the images x and y are within a range defined by the size of the two objects.

    Edit:
    Don't give up, it gets easier!
     

Share This Page