Cris
Cris

Reputation: 12204

How to move a NSObject to a Point(x,y)?

I have a Sprite object defined as follows:

@interface Sprite : NSObject {
CGFloat x;                      // x location
CGFloat y;                      // y location
CGFloat r;                      // red tint
CGFloat g;                      // green tint
CGFloat b;                      // blue tint
CGFloat alpha;                  // alpha value, for transparency
}

In the initWithCoder function of the containing view i instanciated it using:

sprite = [[Sprite alloc] init];
sprite.x = 50; 
sprite.y = 100; 
sprite.width = 100; 
sprite.height = 200; 
sprite.r = 0.0; 
sprite.g = 1.0; 
sprite.b = 0.0;

and I correctly see a green rectangle. Now I would like to move it. I defined a moveTo method like this

- (void) moveTo: (CGPoint) p
{
    x = p.x;
    y = p.y;
}

But when I call it nothing happens. I don't know how to redraw my object.

Upvotes: 0

Views: 325

Answers (2)

Luca Bernardi
Luca Bernardi

Reputation: 4199

NSObject aren't drawable objects. If you want to have a drawable object you should use UIView or its subclass that have all methods for drawing itself like drawRect

Upvotes: 1

Nava Carmon
Nava Carmon

Reputation: 4533

How do you see a green rect? where do you draw it? NSObject cannot draw...

You could derive it from a UIView and change your moveTo to be like this:

- (void) moveTo: (CGPoint) p
{
   self.frame.origin = p;
   [self setNeedsDisplay];
}

Upvotes: 4

Related Questions