Ok so I'm designing a game that has moving views that need to receive touch input. As far as I can tell a view won't register the touchesBegan: method while running the animateWithDuration: method. Also I have a game loop like this
but if I ask my game loop to move the view like this
aSubview.frame.origin.y -= 5;
where aSubview is an initialized instance of a subclass of UIView
I get the error Lvalue required as left operand of assignment
So I guess my question is can anyone tell me which way is easier/better for moving a UIView while keeping the touchesBegan: method active (using animateWithDuration: or using my game loop) and also how to get either of these ways to work. Thanks for any help and direction.
That code - and the error message you get - is the same you'd see for this code. It's a little hard to see why though:
Code:
10 = 10 + 5;
Objective-C uses the same syntax - dot syntax - for properties that C uses to access structs. This causes confusion when you mix the two. In your case aSubview is an object, and you are using the "frame" property. The contents of that property is a struct though (CGRect is a struct, not an object) and you get a read-only copy of that struct.
You need to do this the long way around - copy the struct into a local variable, change it, then copy it back into the object.
but when I try to move the three in my game loop like
int i = 0;
while (i < 3){
CGRect tempRect = [subviewArray objectAtIndex:i].frame;
tempRect.origin.y -= 5;
[subviewArray objectAtIndex:i].frame = tempRect;
i++;
}
I get an error "request for member 'frame' in something not a structure or union". if I try
while (i < 3){
CGRect tempRect = [[subviewArray objectAtIndex:i] frame];
tempRect.origin.y -= 5;
[[subviewArray objectAtIndex:i] frame] = tempRect;
i++;
}
I get no error for line 2, but I get "Lvalue required as left operand of assignment" for line 4. Now I think this is like you said before, .frame returns a read only CGRect which is a struct but can you direct me on how I can use an array and cycle through using a loop to move each subview.
ok, but this code creates new instances mirroring the objects in the array. Is it not possible to add a view from an array to the stage, as its called in actionscript, not sure what its called in ios but I just mean the highest level view, then continue to manipulate it by [subviewArray objectAtIndex:], or does adding the subview remove the option of accessing its properties through the array. My thinking could be completely flawed but I would much appreciate a short description of why this will or won't work.
My code doesn't create new instances at all. You're looking at this line:
Code:
UIView *myView = [subviewArray objectAtIndex:i];
That does *not* create a new UIview. It creates a new pointer, and points it to an existing object in the array.
Your problem stems from a confusion between pointers and objects, and that's partly our fault. We say "call a method on myView" or "myView is a UIView" when we really mean "call a method on the object pointed to by myView" or "myView is a pointer which points to a UIView." It's convenient shorthand, but misleading to anyone new to pointers.
will this create a memory leak because now there is no pointer, and therefore, no way to access the original thisSubview instance, assuming that I had not created the newASubview pointing to it
by the way thanks for all your help so far, youve really cleared up some of my misconceptions about ios, cocoa, c++, c and probably more
Now no one has a pointer to your new view, so no one can release it. It is definitely a leak. Even if you do [bSubview release] later in this code it doesn't help, because you're just calling the method on nil, which has no effect.
As Obi-wan said, you've just take the first step into a larger world!