here is my code:
#import "VirusViewController.h"
#import "MainGame.h"
#import "cocos2d.h"
#implementation VirusViewController
#synthesize window;
- (void)loadView {
// Initialization code
CC_DIRECTOR_INIT();
CCDirector *director = [CCDirector sharedDirector];
//landscape
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setDisplayFPS:YES];
//turn on multi-touch
EAGLView *cocosView = [director openGLView];
[cocosView setMultipleTouchEnabled:YES];
self.view = cocosView;
//default texture formats...
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
[[CCDirector sharedDirector] runWithScene:[MainGame scene]];
}
-(void)viewDidLoad
{
UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(40, 40, 64, 64)];
[nextButton setImage:[UIImage imageNamed:#"homeButton.png"] forState:UIControlStateNormal];
[nextButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nextButton];
[nextButton release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[[CCDirector sharedDirector] purgeCachedData];
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[[CCDirector sharedDirector] release];
[window release];
[super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
-(void)goBack
{
[self.navigationController popViewControllerAnimated:YES];
}
nextButton is not receiving touch events when i init the CC_Director_INIT();
also - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
is not working inside a subview of MainGame.h
in other words... I can't receive any touch events
Try giving your layer these methods
- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
[super onEnter];
}
- (void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
sharedDispatcher is deprecated - you should use this instead:
[[CCDirector sharedDirector] touchDispatcher]
Related
first try and bit stuck on this one...
I Have a ViewController that I wish to display an SKScene in its view
I've tried everything I can think of but its not showing
add SpriteKit/SpriteKit.h
Main.storyboard has a single view controller (myViewController).
myViewController's view - change class to be SKView. (see pic below)
Viewdidload
link to SKScene I wish to load (HelloScene) #import "HelloScene.h"
//ViewController.m
#import "myViewController.h"
#import <SpriteKit/SpriteKit.h>
#import "HelloScene.h"
#interface myViewController ()
#end
#implementation myViewController
- (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = (SKView *) self.view;
spriteView.showsDrawCount = YES;
spriteView.showsNodeCount = YES;
spriteView.showsFPS = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
HelloScene.h
#import <SpriteKit/SpriteKit.h>
#interface HelloScene : SKScene
#end
HelloScene.m
#import "HelloScene.h"
#interface HelloScene ()
#property BOOL contentCreated;
#end
#implementation HelloScene
- (void)didMoveToView: (SKView *) view
{
[self LoadSceneContents];
}
- (void)LoadSceneContents
{
self.backgroundColor = [SKColor blueColor];
self.scaleMode = SKSceneScaleModeAspectFit;
[self addChild: [self newHelloNode]];
}
- (SKLabelNode *)newHelloNode
{
SKLabelNode *helloNode = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster"];
helloNode.text = #"Hello, World!";
helloNode.fontSize = 42;
helloNode.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
return helloNode;
}
#end
Above:Selecting the view controller’s view object and change its class to SKView
Had missed to configure the scene properly...
// Create and configure the scene.
HelloScene *theScene = [HelloScene sceneWithSize:spriteView.bounds.size];
theScene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[spriteView presentScene:theScene
];
Hope it helps somebody else
I enabled myLocation and myLocationButton,
but it not showing on my view.
And I try log _mapView.myLocation it's nil.
I need to showing myLocation dot and myLocationButton.
Have some one can help me, thanks!
//
// ViewController.m
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <GoogleMaps/GoogleMaps.h>
#import "Reachability.h"
#interface ViewController ()<GMSMapViewDelegate,CLLocationManagerDelegate>
{
CLLocationManager * locationManager;
CLLocation *currentLocations;
Reachability * udnReach;
};
#property (strong,nonatomic) GMSMapView * mapView;
#property (strong,nonatomic) NSDictionary * dic;
#property (nonatomic,strong) NSTimer * updatetimer;
#end
#implementation ViewController {
}
- (void)viewDidLoad {
locationManager =[[CLLocationManager alloc]init];
locationManager.delegate =self;
if ([locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[locationManager requestAlwaysAuthorization];
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[locationManager startUpdatingLocation];
_mapView.delegate = self;
_mapView.myLocationEnabled = YES;
_mapView.settings.myLocationButton = YES;
//Map
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude
longitude:locationManager.location.coordinate.longitude
zoom:16];
_mapView= [GMSMapView mapWithFrame:CGRectZero camera:camera];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(networkStatusChanged:) name:kReachabilityChangedNotification object:nil];
udnReach=[Reachability reachabilityWithHostName:#"google.com"];
// udnReach = [Reachability reachabilityForInternetConnection];
[udnReach startNotifier];
[self updatebike];
self.view = self.mapView;
NSLog(#"User's location: %#", _mapView.myLocation);
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
}
i find to show button and get userlocation
// Rather than setting -myLocationEnabled to YES directly,
// call this method:
- (void)enableMyLocation
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined)
[self requestLocationAuthorization];
else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted)
return; // we weren't allowed to show the user's location so don't enable
else
[self.view setMyLocationEnabled:YES];
}
// Ask the CLLocationManager for location authorization,
// and be sure to retain the manager somewhere on the class
- (void)requestLocationAuthorization
{
_locationAuthorizationManager = [[CLLocationManager alloc] init];
_locationAuthorizationManager.delegate = self;
[_locationAuthorizationManager requestAlwaysAuthorization];
}
// Handle the authorization callback. This is usually
// called on a background thread so go back to main.
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status != kCLAuthorizationStatusNotDetermined) {
[self performSelectorOnMainThread:#selector(enableMyLocation) withObject:nil waitUntilDone:[NSThread isMainThread]];
_locationAuthorizationManager.delegate = nil;
_locationAuthorizationManager = nil;
}
}
I'm trying to make an alert view so that upon selecting "Yes" (one of the buttons in AlertView), the user is directed to a new view controller with a button which I made via Storyboard. My code for the IBAction is as follows:
- (IBAction)alertButton:(UIButton *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Welcome Back to SymTrack!" message:#"Did you have reoccuring symptoms?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
}
This is the code for the clickedButtonAtIndex which gives me the empty view controller without the button:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
UpdateViewController *updateVC = [[UpdateViewController alloc] init];
[self presentViewController:updateVC animated:YES completion:nil];
}
}
I've also tried these other codes for clickedButtonAtIndex but I got NSException for all of them:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
UpdateViewController *updateVC = [self.storyboard instantiateViewControllerWithIdentifier:#"UpdateViewController"];
[self presentViewController:updateVC animated:YES completion:nil];
}
}
or
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
UpdateViewController *updateVC = [self.storyboard instantiateViewControllerWithIdentifier:#"UpdateViewController"];
UINavigationController *alertNC = [[UINavigationController alloc] initWithRootViewController:updateVC];
[self presentViewController:updateVC animated:YES completion:nil];
}
}
note: all this code is within the view controller with the button that upon clicking brings the alert view up.
Thanks in advance!
My app has 3 view controllers. VC#3(*Sponsor) takes the user to my website & so I am trying to implement Reachability for this VC. I added System Configuration framework & Apples Reachability .H & .M files to the project. Disabled ARC for Reachability .M file & VC#3 .M file. The project B&R fine, but I still do not get the pop up alert when disconnected from internet. I imported Reachability.h to VC#3 also. What am I missing?
VC#3(*Sponsor).M
#import "Sponsor.h"
#import "ViewController.h"
#interface Sponsor ()
#end
#implementation Sponsor
-(void)viewDidLoad {
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL
URLWithString:#"http://opticertificationprep.webs.com/"]]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(IBAction)home:(id)sender{
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (BOOL) hasInternet {
Reachability *reach = [Reachability
reachabilityWithHostName:#"www.opticertificationprep.webs.com"];
NetworkStatus internetStats = [reach currentReachabilityStatus];
if (internetStats == NotReachable) {
UIAlertView *alertOne = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Check Your Internet Connection" delegate:self cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alertOne show];
[alertOne release];
}
return YES;
}
For anyone with same problem. I was missing this bit of code in the the .M file. Once I added it my pop up alert works great.
`
-(void)viewDidLoad {
{
[super viewDidLoad];
[self hasInternet];
}
`
I'm trying to load google maps view in iOS following the guide here
I created a new viewController for the map view called iptechMapViewController.h , iptechMapViewController.m and iptechMapViewController.xib
Here is the code in iptechMapViewController.m :
#import "iptechMapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#interface iptechMapViewController ()
{
GMSMapView *mapView_;
}
#end
#implementation iptechMapViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
longitude:151.2086
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = mapView_;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Then in another view controller named iptechFlipsideViewController.m I'm trying to show the iptechMapViewController as follows:
- (IBAction)showMap:(id)sender
{
iptechMapViewController *controller = [[iptechMapViewController alloc] initWithNibName:#"iptechMapViewController" bundle:nil];
[self.view addSubview:controller.view];
}
But after clicking the showMap button which is connected to the IBAction showMap, and after iOS asks permission to access my location alert appears and I touch OK, after that nothing shows up and in the console I get the following error"
Failed to make complete framebuffer object 8cd6
Thanks
Passing CGRectZero to mapWithFrame only works if the map view is the root view controller's root view - in which case it is resized to fit the screen. In your case since the map is a subview, you will need to specify a size when creating the map view.