John
John

Reputation: 71

Rotating sprite node from plist

I'm new to Spritekit and I'm creating a game where I want my sprite nodes to rotate when the user touches them. Because the game has many levels and the sprite nodes will all rotate in different angles, I want to place the individual float values in a plist file for the nodes to read their angle rotations from when the user touches them.

I know, ideally to effectively rotate the sprites you would write something like this

sprite.zrotation = M_PI/.5

but I want to call the float values from the plist, and I'm not sure how to properly store the rotation float values in the plist and how to make the spritenodes call their zrotation from the plist. Can somebody please help me?

Upvotes: 1

Views: 99

Answers (1)

Whirlwind
Whirlwind

Reputation: 13665

Assuming that randomizing those angles is not what you want and you need to have all angles for each level stored somewhere, you can do something like this:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// saving a float
[defaults setFloat:0.123 forKey:@"someKey"];

[defaults synchronize];

And to retrive values, you can do something like this:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// getting an Float
float myFloat = [defaults floatForKey:@"someKey"];

Keep in mind that user can easily alter the .plist file which is used by NSUserDefaults class. If this bothers you (probably not), you can hardcode all the values inside an NSArray, or NSDictionary.

EDIT:

Here is how you can read/write values using .plist file. When I posted an answer, I haven't noticed that you are actually asking about .plist file and not about NSUserDefaults (which uses .plist file too). So , sorry for that, my bad :)

And here is an example of hardcoding values:

#import "GameScene.h"

@implementation GameScene{

    NSMutableArray *levels;

}

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */

    levels = [NSMutableArray array];

    NSDictionary *level_1 =
    [NSDictionary dictionaryWithObjects:@[@(0.1f),@(0.01f),@(0.001f)] forKeys:@[@"key_1",@"key_2",@"key_3"]];

    NSDictionary *level_2 =
    [NSDictionary dictionaryWithObjects:@[@(0.2f),@(0.02f),@(0.002f)] forKeys:@[@"key_1",@"key_2",@"key_3"]];

    NSDictionary *level_3 =
    [NSDictionary dictionaryWithObjects:@[@(0.3f),@(0.03f),@(0.003f)] forKeys:@[@"key_1",@"key_2",@"key_3"]];



    [levels insertObject:level_1 atIndex:0];
    [levels insertObject:level_2 atIndex:1];
    [levels insertObject:level_3 atIndex:2];

    NSString *key = @"key_1";
    NSUInteger level = 1;

    if([self isAngleAvailableForKey:key andLevel:level]){

        CGFloat angle = [self getAngleForKey:key andLevel:level];

        NSLog(@"Angle for level %lu and key %@ is %f",(unsigned long)level,key,angle);

    }else{
        NSLog(@"Error, wrong level(%lu) or key(%@)",(unsigned long)level, key);
    }


}


//Check to see if there is such a level or key
-(BOOL)isAngleAvailableForKey:(NSString*)key andLevel:(NSUInteger)level{



    if(level < 1 || level > [levels count]){return NO;}

    if([[levels objectAtIndex:level-1] valueForKey:key] == nil){return NO;}

    return YES;

}
//If already is good, get the angle for given level and key
-(CGFloat)getAngleForKey:(NSString*)key andLevel:(NSUInteger)level{


    return [[[levels objectAtIndex:level-1] valueForKey:key] floatValue];

}

My personal preference would be hardcoding the values if the amount of data is not huge. But that's up to you to decide.

If there is a lot of data, then .plist file would be probably better choice because it can save you from unnecessary memory consumption.

If there is a very small amount of data, then I guess you can even use NSUserDefaults, but this should be the last option because NSUserDefaults are meant for storing the preferences or something like that.

Hope this helps.

Upvotes: 1

Related Questions