How to make sure finger is actually on the screen with cocos2d - ios

I want to test to make sure there is actually a finger on the screen every few seconds. Currently, I have things happening when ccTouchesBegan, though, what happens is you can turn off your device and turn it back on and it still thinks a finger is on the screen, or at least the functions are still going. Is there a way to prevent this? Like creating an #selctor to test this?

This is what I would do....
In my implementation, I would create a bool called 'isItStillBeingTouched'
HelloWorldLayer.h
#interface HelloWorldLayer
#property bool isItStillBeingTouched; // Should be fine anywhere between interface & end
#end
Although you haven't said, I'm guessing that you have loops that are performing the task(s) you want to achieve, hence why they are not stopping of their own accord. What you may try is to wrap it all in a Do While loop:
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
isItStillBeingTouched = YES;
while (isItStillBeingTouched) {
// Your current code....
}
}
You'll need to access the isItStillBeingTouched bool of your HelloWorldLayer class, from the AppDelegate, so alter said class to include this method within the helloWorldLayer.m
+ (id)sharedManager {
static HelloWorldLayer *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
Then in the HelloWorldLayer.h, insert +(id)sharedManager; anywhere tidy between your #interface HelloWorldLayer and your #end statements. Not inside any { or }.
The next step is to change the bool isItStillBeingTouched to NO when the application goes into the background.
Goto your AppDelegate.m, and find the:
-(void) applicationDidEnterBackground:(UIApplication*)application
Alter this to include setting your isItStillBeingTouched bool to NO (within the if statement), and your methods should stop running.
You do this by first calling an instance of the HelloWorldLayer, and then changing the property to NO. For that you'll need to add #import "HellowWorldLayer.h"
Assuming you've never previously altered the AppDelegate, it should look something like this:
#import "AppDelegate.h"
#import "IntroLayer.h"
#import "HellowWorldLayer.h"
//...The stock appDelegate code...
-(void) applicationDidEnterBackground:(UIApplication*)application
{
if( [navController_ visibleViewController] == director_ )
{
[director_ stopAnimation];
HelloWorldLayer *myLocalInstanceOfTheHelloWorldLayerClass;
myLocalInstanceOfTheHelloWorldLayerClass.levelToLoadUp = NO;
}
}

Related

Changing Variable in another class iOS

I have follow countless similar situations on here ( stack overflow ). I have applied those write ups for my situation and have come close but haven't found a solution that seems work for me. As this is something very trivial I am baffled at how it isn't working for me.
Anyways, I am trying to set a Int variable from inside the appDelegate class and the variable actually belongs, if that is the right word, to another class (mainViewController).
For the MainViewController class .h file I have
#interface MainWindowControllerViewController :... {
int relayState;
}
#property (readwrite, nonatomic) int relayState;
For the MainViewController class .m file I have
#implementation MainWindowControllerViewController
#synthesize relayState = _relayState;
-(void)quickConnect { // Call for all UI bypassing and quick kill
NSLog(#"Relay state in MainWindow = %d", relayState);
if (_relayState == 1) {
NSLog(#"TURNING ON KILL SWITCH");
self.writeData = [[NSString stringWithFormat:#"%#", #"e"] dataUsingEncoding:NSASCIIStringEncoding];
[sensor write:sensor.activePeripheral data:writeData];
} else {
NSLog(#"TURNING OFF KILL SWITCH");
self.writeData = [[NSString stringWithFormat:#"%#", #"o"] dataUsingEncoding:NSASCIIStringEncoding];
[sensor write:sensor.activePeripheral data:writeData];
}
}
For the appDelegate.h file I have
#import "MainWindowControllerViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
MainWindowControllerViewController *relayState;
}
#property (nonatomic, assign) MainWindowControllerViewController *relayState;
Then for the appDelegate.m file I have
#import "MainWindowControllerViewController.h"
#implementation AppDelegate
#synthesize relayState = _relayState;
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *) sourceApplication annotation:(id)annotation {
NSLog(#" Calling application: %#", sourceApplication);
NSLog(#" URL scheme: %#", [url scheme]);
if ([sourceApplication isEqualToString:#"URL-Scheme-Test"]) {
if ([[url scheme] isEqualToString:#"TurnOffRelay"]) {
_relayState =[[MainWindowControllerViewController alloc] init];
_relayState.relayState = 1;
NSLog(#"Relay State BEFORE = %d", _relayState.relayState);
}
return YES;
if ([[url scheme] isEqualToString:#"TurnOnRelay"]) {
_relayState =[[MainWindowControllerViewController alloc] init];
_relayState.relayState = 0;
NSLog(#"Relay State BEFORE = %d", _relayState.relayState);
}
return YES;
}
else
return NO;
}
This is pretty much it. When I do the logs I find that the relayState that I want to change the value of shows that I was able to change the value. But when I do the If statement above that checks weather the relayState is equal to 1 the variable is always 0.
I'm not the most proficient in Obj C. Some help would be appreciated! Thanks. I'll gladly answer any extra questions about code if needed.
You are kind of mixing some older style obj-c with how it usually looks currently and I think you are hiding a variable (one declaration is hiding another)
Take out:
{
int relayState;
}
From your MainWindowControllerViewController interface
Take out the #synthesize in the MainWindowControllerViewController implementation
Don't use _relayState (just use relayState)
Do the same with the MainWindowControllerViewController variable in the AppDelegate.
If that doesn't fix it, we need to see what is happening to the VC you are creating.
The problem I believe is this line
_relayState =[[MainWindowControllerViewController alloc] init];
You are initializing MainWindowControllerViewController in both the if blocks which is resetting the value to the default value (In this case 0) every time.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MainWindowControllerViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MainWindowControllerViewControllerIdentifier"];
vc.relayState = 1;
For the MainViewController class .h
#interface MainWindowControllerViewController :... {
int relayState; //remove this line
}
#property (nonatomic) int relayState; //use this
try this
You are making a mistake that will create endless problems for you.
You have a property named relayState and an instance variable named relayState.
The property "relayState" is backed by an instance variable named _relayState. You have another instance variable named relayState. Two different variables. Assigning to self.relayState changes one, assigning to relayState changes the other. Don't do it.
Just get rid of the "int relayState;" and don't use #synthesize and you'll be fine. At least a lot finer than you are now. You can then either use self.relayState or _relayState. At least you will always know what you are doing.
PS. Having an int relayState and a MainWindowViewController* relayState is just asking for trouble. You seem to be intentionally trying to confuse yourself.
PS. You seem to do the same dangerous game with the writeData property/variable.

iOS singleton viewDidLoad empty and on viewDidAppear not

I created a singleton in ios7 like this:
SharedData.h
#interface SharedData : NSObject
{
}
+ (id)sharedInstance;
#property (strong, nonatomic) NSMutableArray *list;
#end
SharedData.m
#import "SharedData.h"
#implementation SharedData
#synthesize list;
// Get the shared instance thread safe
+ (SharedData *)sharedInstance {
static dispatch_once_t once = 0;
static SharedData *sharedInstance = nil;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (id)init {
self = [super init];
if (self) {
//initialize
list = [[NSMutableArray alloc] init];
}
return self;
}
#end
I always use this code to access this class:
SharedData *sharedData = [SharedData sharedInstance];
The problem is now when I switch the view in my viewDidLoad method the list is empty but in my viewDidAppear method everything is fine. Any ideas?
EDIT:
This is the code how I change the views:
SharedData *sharedData = [SharedData sharedInstance];
//clear feed and add new feed
[sharedData.list removeAllObjects];
[sharedData.list addObjectsFromArray:newList];
//show new gui
[self.navigationController performSegueWithIdentifier:#"goToMain" sender:self];
NOTE: I push from a normal ViewController to a TabBarController -> NavigationController -> TableViewController to display the list.
I guess you have the confusion between these two viewcontroller methods:
-(void)viewDidLoad{
//
}
&
-(void) viewDidAppear{
//
}
viewDidAppear is the method which is called each time your view changes but viewDidLoad is the method which is not necessarily called each time your view changes.
ViewDidLoad method is called when view loads for the first time, after that it doesn't get called until the views are removed/released.
P.S: I suggest you to put the breakpoint in your viewDidLoad and viewDidAppear method and feel it. Your answer lies there.
Hope this helps you alot.
Good Luck.
The problem was i created a segue which went from the button to the next view. Because of this the viewDidLoad gets earlier called than the list assigned. I just changed the segue to go from view to view.
How are you changing from one viewController to the other? Wich classes are the parents of your destination ViewController?,
If you are modifying properties of the view in the prepareForSegue method... you are forcing the view to load.
For example, you are setting the list of your singleton in prepareForSegue, but before setting the list you are modifying a property of your destination viewController. (doing something like destVC.view = XXX or destVC.viewControllers = XX if you are subclassing a UITabBarViewController...) Then you are triggering the viewDidLoad method , and it's executing before you have set the list to the correct value.
Or maybe you are seguing in two different places to the destinationViewController. And when the viewDidLoad happens, you still have not updated the list on the singleton.
Here is the transcription of the chat with the poster of the question: https://chat.stackoverflow.com/transcript/55218

Pass BOOL value

I am working on an app and I got stuck at the point where I can't seem to retrieve the value of a BOOL set in a class.
I spent too much time already on it, been through all the questions I found that seem to cover the matter.
The bad thing here is that I get something, but not what I need (I get a 0, which means, I guess, that the value wasn't retrieved correctly as it should be 1).
The things I tried are :
pass a pointer to my first class and access to my BOOL like this:
//in some method
self.pointerFirstClass.myBOOL;
NSLog(#"%d", firstClass.myBOOL); => This gives 0!
by declaring it (talking of the pointer) as a property in my second class (and importing the h. file from my first class, where my BOOL is declared as property too):
#property FirstClass *pointerFirstClass;
But I got 0 using this.
The other shot I gave was add my BOOL in the first class and create an instance of the class in my second class
//in some method
FirstClass *firstClass = [[FirstClass alloc] init];
if (firstClass.myBOOL){
//Do something
}
NSLog(#"%d", firstClass.myBOOL); => This gives 0!
But I got 0 too.
As Booleans are primitive types, like in C, I get a bit confused since I am new to object-oriented programming, I don't know how I could like create a getter for this, for example.
I also tried to do a - (BOOL)getBOOLValue method in my first class, and call this method in my second class and assign it to a BOOL in that second class.
But the result wasn't better.
Am I missing something?
Is there a way to get my value that I didn't think of or didn't know about yet?
I am running low on thoughts on how to get around this, it shouldn't be that hard IMO so I hope it is something simple that I just left aside.
EDIT :
Some actual code. I am working between 2 files called AppDelegate (yes, the actual one) and WelcomeViewController (so a VC).
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
BOOL inRegion; //thought of this making my BOOL as a property of AppDelegate
}
#property (strong, nonatomic) UIWindow *window;
#property BOOL inRegion; //Declaring my BOOL here to make it accessible for another class
- (BOOL)getBOOLValue; //An attempt to pass my BOOL value
AppDelegate.m
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if (state == CLRegionStateInside)
{
self.inRegion = YES; //Set my BOOL to TRUE
}
else if (state == CLRegionStateOutside)
{
self.inRegion = NO; //Else set it to False
}
- (BOOL)getBOOLValue
{
return inRegion; //Tried to create a custome "getter"
}
WelcomeViewControler.m (I changed nothing in the .h file)
I said I tried many things, right now, this is the last version of my code.
//Simply trying to do a Segue on a condition...
- (IBAction)onClick:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init];
if (appDelegate.inRegion) {
[self performSegueWithIdentifier:#"WelcomeToDetection" sender:self];
}
else
{
//Do Nothing
}
}
As said, I want to retrieve the BOOL value of the AppDelegate.
Thank you.
This code doesn't make sense:
self.pointerFirstClass.myBOOL;
NSLog(#"%d", firstClass.myBOOL); => This gives 0!
The first line doesn't do anything. You're not assigning anything to the property, and you're not doing anything with the value. Furthermore, the second line doesn't relate to the first line in any way that we can see from the code you've provided. Try this instead:
self.pointerFirstClass = [[FirstClass alloc] init];
self.pointerFirstClass.myBOOL = YES;
NSLog(#"myBOOL = %d", self.pointerFirstClass.myBOOL);
In other words, you need to be sure that self.pointerFirstClass points to a valid object. And then you need to make sure that you've assigned the value you want to the myBOOL property of that object.
Update: This looks like a case where you're talking to the wrong object. Look at this:
- (IBAction)onClick:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init];
This is surely not what you really want. The application object is a single object -- a real singleton, in fact, meaning that there is and can be only one application object. That object has a delegate object, and that's a specific instance of your AppDelegate class. In this code, though, you're creating a new instance of AppDelegate, one that's different from the one that the application is using. Any changes that are made to the actual application delegate in response to messages from the application will not be reflected in the new object that you've created.
What I think you want is to get the actual application delegate object, and you can do that using:
[[UIApplication sharedApplication] delegate];
So, change your code to look like this:
- (IBAction)onClick:(id)sender {
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];;
if (appDelegate.inRegion) {
[self performSegueWithIdentifier:#"WelcomeToDetection" sender:self];
}
// note: you don't need an else clause if it doesn't do anything
}
That way, you'll be talking to the same object that the app uses, which is the one that has the inRegion property set in response to the location manager call.
UPDATE - Now we can see your code the problem is obvious, you are trying to access the appDelegate by creating a new one...
- (IBAction)onClick:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init];
Instead you should be doing this....
- (IBAction)onClick:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]
--
Not sure if you are posting your actual code? but the first example you give...
self.pointerFirstClass.myBOOL;
NSLog(#"%d", firstClass.myBOOL); => This gives 0!
Shouldn't the second line be
NSLog(#"%d", self.pointerFirstClass.myBOOL);
Also this property...
#property FirstClass *pointerFirstClass;
Won't retain it once you've set it, it needs to be
#property (nonatomic,strong) FirstClass *pointerFirstClass;
In the second example...
FirstClass *firstClass = [[FirstClass alloc] init];
if (firstClass.myBOOL){
//Do something
}
NSLog(#"%d", firstClass.myBOOL); => This gives 0!
You allocate and initialise a new FirstClass object and then check the property straight away, if you are not setting this to YES in the init then it will be false
Like I say, maybe you're not posting your actual code?
I guess what you want is initializing myBOOL to 1.
If so, you need do something as following
#implement FirstClass
- (id)init
{
self = [super init];
if(self) {
_myBOOL = 1;
}
return self;
}
// Other methods
#end
EDIT:
The comments is why you get 0.
- (IBAction)onClick:(id)sender {
AppDelegate *appDelegate = [[AppDelegate alloc] init]; // this is the problem.
// you create a new appdelegate,
// and never call locationManager:didDetermineState:forRegion:
if (appDelegate.inRegion) {
[self performSegueWithIdentifier:#"WelcomeToDetection" sender:self];
}
else
{
//Do Nothing
}
}
rewrite your code as following:
- (IBAction)onClick:(id)sender {
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
if (appDelegate.inRegion) {
[self performSegueWithIdentifier:#"WelcomeToDetection" sender:self];
}
else
{
//Do Nothing
}
}

Can't Access Function from another Class in Cocos2D

I've implemented 3 classes.
-Scene.m & .h
Scene.m: (has HudLayer & BackgroundLayer properties on the header file)
-(id)init{
self = [super init];
if(self != nil){
//Level1Layer
_level1GameplayLayer = [Level1Layer node];
[self addChild:_level1GameplayLayer z:0];
//Hud Layer
_hudLayer = [HUDLayer node];
[self addChild:_hudLayer z:1];
}
return self;
}
-Which holds these 2 layers
BackgroundLayer.m & .h && HUDLayer.m & .h
Everytime I click the screen I get a Log notice like this "Touched Screen"(called on backgroundlayer.m) which is followed by a function which is implemented on HUDLayer.m & .h
I call it like this:
[_hud getAmmo:self.ammoLeft magsLeft:self.magsLeft];
_hud is stated on the Scene.m like this:(and its imported on backgroundlayer.m)
HUDLayer *hudLayer = [HUDLayer node];
[self addChild:hudLayer z:1];
background is z:0.
Also BackgroundLayer.m has the property under #interface:
(I realized this is nil because its not initialized, How do I initialize this???)
#property (strong) HUDLayer *hud;
Between the call of getAmmo: I make 3 CLOGS, one BEFORE "We're about to getAmmo:"
one INSIDE the function on HUDLayer.m that calls "Inside getAmmo:"
and one AFTER "We gotTheAmmo:"
BackgroundLayer.m:
CCLOG(#"We're about to getAmmo");
_hud getAmmo:self.ammoLeft magsLeft:self.magsLeft];
CCLOG(#"We got the ammo:%d, mags:%d",self.ammoLeft,self.magsLeft);
HUDLayer.m:
-(void)getAmmo:(int)ammo magsLeft:(int)magsLeft
{
CCLOG(#"We did this");
hudMagsLeft=magsLeft;
hudAmmoLeft = ammo;
CCLOG(#"HUD MAGS: %d, AMMO:%d", hudMagsLeft,hudAmmoLeft);
}
Im only getting the one before and the one after, there's no warnings on the way the function is being called but for some reason it isn't being called. There's no if statements or anything..what is it Im doing wrong???
So to make the question more simple, how do I access properties/functions from other classes?
Thank you for your time, have a good one.
On the scene.h I added this function:
-(id)initWithHUD:(HUDLayer *)hud;
Then on the scene.m I made this:
_backgroundLayer = [[[BackgroundLayer alloc] initWithHUD:_hudLayer] autorelease];
on Backgroundlayer.m I changed regular init with
-(id)initWithHUD:(HUDLayer *)hud{
:D

Variable lost after dismissing modalViewController

I'm using Reachability in my iPad app and discovered some issues when using modalViewControllers.
In my mainViewController I have a BOOL variable determining weather I'm online or not. Here's my code:
// mainViewController.h
BOOL online;
// mainViewController.m
- (void)reachabilityChanged:(NSNotification *)note
{
if([[note object] isReachable]) {
online = YES;
}
else {
online = NO;
}
}
- (void)getOnline
{
NSLog(#"%d", online);
}
// modalViewController.m
#import "mainViewController.h"
- (IBAction)dismissMe
{
mainViewController *main = [[mainViewController alloc] init];
[main getOnline];
[self dismissModalViewControllerAnimated:YES];
}
When I'm calling [self getOnline] within the mainViewController, it returns 1 ('cause I am online).
But: when I'm calling [main getOnline] within the modalViewController, it returns 0 in the log.
Does anybody know why?!
I also tried to put the online variable as a #property into the modalViewController to handle the if online stuff within the modal. But when I assign a value to it (from the main), and log it within the modal, it always returns (NULL).
Hope, you can help me! With best regards, Julian
Short answer: because they use different instances of the online variable.
Long answer: you should only declare BOOL online in the header, not define it. Defining should happen in the .m file, like this:
In the mainViewController.h:
extern BOOL online; // Declare the variable
In the mainViewController.m:
BOOL online; // Define the variable
// the rest of your code
The way your code is written, a separate BOOL online is created for each .m file that includes mainViewController.h; I am sure this is not what you intended.

Resources