Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:34:33+00:00 2026-05-27T14:34:33+00:00

#import HelloWorldLayer.h #import Player.h #import MyObject.h //#import SettingsManager.h // HelloWorldLayer implementation @implementation HelloWorldLayer @synthesize

  • 0
#import "HelloWorldLayer.h"
#import "Player.h"
#import "MyObject.h"
//#import "SettingsManager.h"

// HelloWorldLayer implementation
@implementation HelloWorldLayer
@synthesize myObject,newPlayer;

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {
        usrDef = [NSUserDefaults standardUserDefaults];
        //CGSize
        CGSize winSize = [[CCDirector sharedDirector]winSize];
        //settingsmangaer
        //myValue = [[SettingsManager sharedSettingsManager] getInt:@"hiscore"];

        //child
        newPlayer = [[Player alloc]init];
        [self addChild:newPlayer];
        CCMenuItem *myMenuItem = [CCMenuItemImage itemFromNormalImage:@"Button0000.png" selectedImage:@"Button0001.png" target:self selector:@selector(buttonPressed:) ];
        CCMenu *myMenu = [CCMenu menuWithItems:myMenuItem, nil];
        myMenu.position = ccp(50, 50);
        [self addChild:myMenu];
        //cclabel
        myValue = [usrDef integerForKey:@"difficulty"];
        myLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Hello: %d",myValue]  fontName:@"Arial" fontSize:50];
        [myLabel setColor:ccBLACK];
       // [myLabel setCString:@"hihi: %d",myValue];
        myLabel.position = ccp(winSize.width/2,winSize.height/2);
        [self addChild:myLabel];

    }

    return self;
}

I have a buttton here to add 1 to myValue when pressed and store it to nsdefaults.

-(void)buttonPressed:(id)sender{
    myValue +=1;
    [myLabel setString:[NSString stringWithFormat:@"Hihi: %d",myValue]];
    [usrDef setInteger:myValue forKey:@"diffculty"];
    [usrDef synchronize];
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don't forget to call "super dealloc"
    [super dealloc];
}

@end

The nsdefault i’m using is not working. When i reopen the application the label starts from 0 again. The program compiles properly and i’m not getting any error. Please help thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:34:34+00:00Added an answer on May 27, 2026 at 2:34 pm
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    -(void)buttonPressed:(id)sender{
        myValue +=1;
        [myLabel setString:[NSString stringWithFormat:@"Hihi: %d",myValue]];
        [defaults setInteger:myValue forKey:@"diffculty"];
        [defaults synchronize];
    }
    

    Above is the proper syntax for what you want to do

    Below is how to retrieve the data.

    - (void)viewDidLoad
    {
        // Get the stored data before the view loads
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
        NSString *firstName = [defaults objectForKey:@"firstName"];
        NSString *lastName = [defaults objectForKey:@"lastname"];
    
        int age = [defaults integerForKey:@"age"];
        NSString *ageString = [NSString stringWithFormat:@"%i",age];
    
        NSData *imageData = [defaults dataForKey:@"image"];
        UIImage *contactImage = [UIImage imageWithData:imageData];
    
        // Update the UI elements with the saved data
        firstNameTextField.text = firstName;
        lastNameTextField.text = lastName;
        ageTextField.text = ageString;
        contactImageView.image = contactImage;
    
        [super viewDidLoad];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import javax.swing.JOptionPane; public class RotateArrayCircularLL { private Node head=null; public void init() { int
import org.apache.tools.ant.Project object HelloWorld { def main(args: Array[String]) { println(Hello, world!) } } I
import sys words = { 1 : 'one', 2 : 'two', 3 : 'three',
import sys sys.path.append('/home/myuser/svn-repos/myproject') from myproject.settings import * But, it says module not found when
import java.util.Collection; import example.Event; public interface Query { public boolean hasMore (); public Collection<Event>
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
import threading event = threading.Event() event.set() print event.wait(1) None event.clear() print event.wait(1) None So
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import wx class MainFrame(wx.Frame): def __init__(self,parent,title): wx.Frame.__init__(self, parent, title=title, size=(640,480)) self.mainPanel=DoubleBufferTest(self,-1) self.Show(True) class DoubleBufferTest(wx.Panel):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.