Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS Game Development
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning iOS Game Development
This is the forum to discuss the Wrox book Beginning iOS Game Development by Patrick Alessi ; ISBN: 978-1-1181-0732-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS Game Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 18th, 2012, 12:25 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ch6: Change the color of the blocks instead of destroying them

Hi there,
I'm somewhat new to objective-c/core graphics, but I'm curious if after initializing and calling the drawRect routine, would it be possible to change the color of the blocks dynamically without destroying them? I believe my issue(s) are mainly to do with the context, but I'm unsure if the inputColor member can be changed past initialization. My function updateColor() is below and would be called when collision is detected on a block. Everything else is verbatim of the CH6 project. Any help or advice considering would be highly appreciated.
Thanks,
J

Code:
- (id)initWithFrame:(CGRect)frame color:(int) inputColor;
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.color = inputColor;
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    float viewWidth, viewHeight;
    viewWidth = self.bounds.size.width;
    viewHeight = self.bounds.size.height;
    
    //Get the drawing context
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //define a rect in the shape of a block
    CGRect blockRect = CGRectMake(0, 0, viewWidth, viewHeight);
    
    //define a path using the rect
    UIBezierPath* path = [UIBezierPath bezierPathWithRect:blockRect];
    
    //set the line width of the path
    path.lineWidth = 0.0;
    
    //define a gradient to use to fill the blocks
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[2] = { 0.0, 1.0 };
    
    CGFloat components[8] = { 0.0, 0.0, 0.0, 1.0, //start color
        1.0, 1.0, 1.0, 1.0 }; //end color
    
    //determine gradient color based on color property
    switch (self.color) {
        case RED_COLOR:
            //red block
            components[0] = 1.0;
            break;
        case GREEN_COLOR:
            //red block
            components[1] = 1.0;
            break;
        case BLUE_COLOR:
            //red block
            components[2] = 1.0;
            break;
        default:
            break;
    }

    CGGradientRef myGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, sizeof(locations)/sizeof(CGFloat));
    
    CGContextDrawLinearGradient(context, myGradient, CGPointMake(0, 0), CGPointMake(viewWidth, 0), 0);

    //clean up the color space and gradient
    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(myGradient);
    
    //stroke the path
    [path stroke];
}

-(void)updateColor:(int) inputColor
{
    self.color = inputColor;
    //Get the drawing context
    CGContextRef context = UIGraphicsGetCurrentContext();
    //define a gradient to use to fill the blocks
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[8] = { 0.0, 0.0, 0.0, 1.0, //start color
        1.0, 1.0, 1.0, 1.0 }; //end color
    CGFloat locations[2] = { 0.0, 1.0 };
    CGGradientRef myGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, sizeof(locations)/sizeof(CGFloat));
    
    CGContextDrawLinearGradient(context, myGradient, CGPointMake(0, 0), CGPointMake(self.bounds.size.width, 0), 0);

}




Similar Threads
Thread Thread Starter Forum Replies Last Post
change color of background meena88 C# 2005 3 December 1st, 2009 04:48 AM
change cell color martin1 Visual Basic 2005 Basics 2 October 4th, 2006 08:55 AM
re: color change jack_in_black ASP.NET 2.0 Professional 0 July 11th, 2006 07:24 AM
COLOR CHANGE IN TEXTBOXES Rashad Javascript How-To 5 November 28th, 2005 06:06 AM
Color change possability? rolandatem VB How-To 2 November 19th, 2004 03:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.