I have seen this alternative to using CGRectMake() in order to initialise a CGRect variable:
CGRect frame = (CGRect){0,0,10,10};
My question is, how does CGRect frame = (CGRect){0,0,10,10}; work? What's going on behind the scenes? It looks like a c-style array is being initialised ({x,y,w,h}) which is then being cast as a CGRect struct - is this correct? If so, how is it possible to cast a c style array as a struct?
N.B. I am not asking if it is appropriate to use the above alternative to CGRectMake(), I only wish to understand why/how it works.
It's a so-called compound literal. You can read more about them in this article by Mike Ash: Friday Q&A 2011-02-18: Compound Literals.
U can use like this:
CGRect rect = CGRectFromString(#"{{0, 0}, {612, 892}}"); // it contents { CGPoint origin;CGSize size;};
NSLog(#"rect : %#",NSStringFromCGRect(rect));
Check this:
CGPoint origin = {10, 20};
CGSize size = {100, 200};
CGRect rect = {origin, size};
Related
I keep seeing a phrase like this:
//Example one
CGPoint backgroundScrollVel = ccp(-1000, 0);
//Another Example
// 3) Determine relative movement speeds for space dust and background
CGPoint dustSpeed = ccp(0.1, 0.1);
CGPoint bgSpeed = ccp(0.05, 0.05);
So what does ccp signify? Is it a property of CCParallax?
Like Stephen said, it's just a macro for CGPointMake(x, y), but if you particularly mean what does "ccp" stand for it's most likely c o c os2d p oint
Are you using Cocos2D? If so, ccp is just a C macro to create a point. As in:
#define ccp(__X__,__Y__) CGPointMake(__X__,__Y__)
It's just a convenience constructor for the CGPoint type.
Pretty sure it's just a macro to CGPointMake, but don't quote me on that.
It's a shorthand macro that maps to CGPointMake(x, y).
Basically it's a way to create CGPoints with less typing.
Nope no difference except CGPointMake is harder to type:
#define ccp(__X__, __Y__) CGPointMake(__X__,__Y__)
Found here:
http://www.cocos2d-iphone.org/api-ref/0.99.3/_c_g_point_extension_8h_source.html
I'm a beginner in quartz.
I'm wonderring a way that recognize if CGPoint is included in shape.
Please give me a help of Expert.
Follow is concept diagram.
In above three case,
Result I want is YES, Because three RED CGPoint is included in shape.
It is possible like follow way?
CGPoint RedPoint1 = {200,100};
CGPoint RedPoint2 = {200,200};
CGPoint RedPoint3 = {350,300};
BOOL includeRect;
includeRect = CGRectContainsPoint(RectCase, RedPoint1);
BOOL includeCircle;
includeCircle = CG ? ContainsPoint(CircleCase, RedPoint2)
BOOL includeBoldLine;
includeBoldLine = CG ? ContainsPoint(BoldLineCase, RedPoint3);
The ease of this all depends on how your shapes are defined.
If you have these as CGPathRefs or NSBezierPaths there is a containsPoint: method you could use.
If these are CGRects that have a transform applied to them, you can use the CGAffineTransformPoint methods to move the point into the same coordinate space and then use CGRectContainsPoint
Is there a way to make nullable struct in objective C like in C# you can use Nullable<T>?
I need a CGPoint to be null when there is no applicable value. I cannot allocate a random invalid value for this like (-5000, -5000) because all values are valid for this.
What if you define a CGPoint using CGPointMake(NAN, NAN) similar to CGRectNull? Surely with NAN's for coordinates, it's not still a valid point.
CGPoint is a struct and that has some different rules in objective-c than you might think. You should consider reading about structs in objective-c.
The way this is done most of the time is to wrap the struct in an object because that object can be set to null. NSValue will wrap a CGPoint.
NSValue * v = [NSValue valueWithPoint:CGPointMake(1,9)];
NSVAlue * vNull = [NSValue valueWithPointer:nil];
if([v objCType] == #encode(CGPoint)) printf("v is an CGPoint");
CGPoint is a enum, not an object. You can use CGPointZero, or you can wrap all of your points inside of NSValue, which are objects and can be nil.
There is also nothing stopping you creating your own struct based on CGPoint, similar to how C# 2 works.
struct NilableCGPoint { bool isNil; CGPoint point; }
Examples of use:
// No value (nil)
NilableCGPoint myNilablePoint.point = CGPointZero;
myPoint.isNil = YES;
// Value of (0,0)
NilableCGPoint myNilablePoint.point = CGPointZero;
myPoint.isNil = NO;
// Value of (100, 50)
NilableCGPoint myNilablePoint.point = CGPointMake(100, 50);
myPoint.isNil = NO;
I want to assign suitSize to scrollButton what I'm doing wrong?
UIView *scrollButton = [suitScrollView viewWithTag:1];
CGSize suitSize =CGSizeMake(10.0f,10.0f);
(UIButton *)scrollButton.frame.size=suitSize;
frame is a property, not a structure field. You can't assign to a subfield of it. Think of it as a function call; dot syntax for properties is convenience.
This:
scrollButton.frame.size = suitSize;
Is equivalent to:
[scrollButton frame].size = suitSize;
Which doesn't work; it doesn't make any sense to assign to a field of a function result.
Instead, do this:
CGFrame theFrame = [scrollButton frame];
theFrame.size = suitSize;
[scrollButton setFrame: theFrame];
Or, if you prefer:
CGFrame theFrame = scrollButton.frame;
theFrame.size = suitSize;
scrollButton.frame = theFrame;
Note that casting the scrollButton to a UIButton isn't necessary; UIViews have frames, too.
Don't mix the property accessors and struct field access on the left side of an assignment.
An lvalue is an expression that can appear on the left side of an assignment. When you mixstructs and properties, the resulting expression is not an lvalue, so you can't use it on the left side of an assignment.
(UIButton *)scrollButton.frame.size=suitSize;
The scrollButton.frame part is a property access. The .size part accesses a field of the frame structure. Steven Fisher's example above is the right way to break up the code to avoid the problem.
When dealing with properties that are structs you cannot directly set sub-structs in this manner...
(UIButton *)scrollButton.frame.size=suitSize;
The frame property of the UIButton is a CGRect struct. The compiler sees your .size access and tries to resolve it to a setter which does not exist. Instead of mixing struct member access with property accessors you need to deal with the CGRect struct type as a whole...
CGRect frame = (UIButton *)scrollButton.frame;
frame.size = CGSizeMake(100, 100);
(UIButton *)scrollButton.frame = frame;
I want to set the Z-order of the sprites I create in Objective-C, specifically in Cocos2D.
This is the error I get when trying to build the following code:
CCSprite *mySprite = [CCSprite spriteWithFile:#"Image.png" rect:CGRectMake(0, 0, 96, 24)];
mySprite.zOrder = 0;
...220: error: object cannot be set - either readonly property or no setter found
Z-Order must be able to be set somehow - can it only be set on the line of instantiation and not after it's been created? Do I have to create a setter method for an attribute for CCSprite? Why wouldn't it already have those methods?
If you need to reorder after adding the sprites, as GamingHorror said, use:
[self reorderChild:sprite z:newZ];
Your answer works if all you need is to set the original order
Figured it out:
When adding the sprite to self, need to add a parameter:
CCSprite *mySprite = [CCSprite spriteWithFile:#"mySpriteImage.png" rect:CGRectMake(0, 0, 96, 24)];
[self addChild:mySprite z:1];
z = 0 is background, the highest z index will be on top of the other sprites
-JJR
_background.name = #"background";
[self addChild:_background];
_background.zPosition =-5;
check out the z position at last line ,i hope this will help you