Attempting to simply add in a joystick to control a sprite side to side, and up and down. The only non-outdated tutorial I can find for this, which is still a year old, simply get's me as far as 'Implicit declaration of function 'ccDrawSolidPoly' is invalid in C99'
.m
Code:
- (id)init
{
// Apple recommend assigning self with supers return value
self = [super init];
if (!self) return(nil);
// Enable touch handling on scene node
self.userInteractionEnabled = YES;
[self initJoyStick];
// done
return self;
}
-(void)initJoyStick
{
SneakyJoystickSkinnedBase *leftJoy = [[SneakyJoystickSkinnedBase alloc] init];
[leftJoy setPosition:CGPointMake(64, 32)];
leftJoy.backgroundSprite = [ColoredCircleSprite circleWithColor:[CCColor colorWithRed:255.0 green:0 blue:0 alpha:120.0] radius:32];
leftJoy.thumbSprite = [ColoredCircleSprite circleWithColor:[CCColor colorWithRed:0 green:0 blue:255.0 alpha:200.0] radius:16];
leftJoy.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0, 0, 128, 128)];
joystick = leftJoy.joystick;
[self addChild:joystick];
}
.h Code:
#import "cocos2d.h"
#import "cocos2d-ui.h"
#import "SneakyJoystick.h"
#import "SneakyButton.h"
#import "SneakyButtonSkinnedBase.h"
#import "SneakyJoystickSkinnedBase.h"
#import "ColoredCircleSprite.h"
#import "ColoredSquareSprite.h"
#interface HelloWorldScene : CCScene
{
SneakyJoystick *joystick;
}
+ (HelloWorldScene *)scene;
- (id)init;
#end
Any ideas or other ways of accomplishing this? I would highly appreciate it!
I had a fun time with this also.
The 2 header files ColoredCircleSprite.h and ColoredSquareSprite.h imported cocos2d.h. They also need to import CCDrawingPrimitives.h
This is the only way I could get it to work. Hope this helps.
Related
I am pretty stuck trying to create a IOS module for Titanium/Appc i am trying to intergrate https://github.com/antiguab/BAFluidView so i can use it in titanium.
I have followed the module tutorials have it working fine with just the standard view but when i try to add BAFluidView it doesnt work.
I have included the classes in xcode and have the code below.
#import "ComExampleFluidView.h"
#import "TiUtils.h"
#import "BAFluidView.h"
#import "UIColor+ColorWithHex.h"
#implementation ComExampleFluidView
- (void)initializeState
{
// Creates and keeps a reference to the view upon initialization
square = [[UIView alloc] initWithFrame:[self frame]];
BAFluidView *view = [[BAFluidView alloc] initWithFrame:view.frame];
[view fillTo:#1.0];
view.fillColor = [UIColor colorWithHex:0x397ebe];
[view startAnimation];
[square addSubview:view];
[self addSubview:square];
[super initializeState];
}
-(void)dealloc
{
// Deallocates the view
RELEASE_TO_NIL(square);
[super dealloc];
}
-(void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
{
// Sets the size and position of the view
[TiUtils setView:square positionRect:bounds];
}
-(void)setColor_:(id)color
{
// Assigns the view's background color
square.backgroundColor = [[TiUtils colorValue:color] _color];
}
#end
header file is
#import "TiUIView.h"
#interface ComExampleFluidView: TiUIView {
UIView *square;
}
#end
Can anyone give some suggestions on this?
since you are trying to bridge a native view, you need some layout helpers that are required to handle the Titanium-layout-system properly. Please check modules like ti.googlemaps, especially the initialization of views. In addition, your custom setters like setColor need to apply the color to your BAFluidView, not to your UIView, so you need to keep a reference of that inside your header. I guess the ti.googlemaps example should explain all concepts you are looking for. Good luck!
The following code won't compile because of the error that results from trying to set the GKLeaderboardViewController delegate to the calling instance.
The error message is:
Assigning to 'id<UINavigationControllerDelegate>' from incompatible type 'MainMenu *'
where MainMenu is of type CCLayer.
If the assignment statement (leaderboard.delegate = self) is commented out, the code will compile, the leaderboard will display, but the callback will not be called when the "done" button is pressed.
This is the code:
- (void) showLeaderBoard {
// Show GC leaderboard
GKLeaderboardViewController *leaderboard = [[GKLeaderboardViewController alloc] init];
if (leaderboard != nil) {
leaderboard.delegate = self;
leaderboard.category = #"ldrbrd_ref";
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
[[app navController] presentViewController:leaderboard animated:YES completion:nil];
}
}
Incidentally, this is my header for the object declaration:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "CCGameButton.h"
#import <GameKit/GameKit.h>
#interface MainMenu : CCLayer <CCGameButtonDelegate, GKLeaderboardViewControllerDelegate> {
}
+ (CCScene *) scene;
#end
What am I doing wrong? Any help appreciated!
I believe what you are looking for is the leaderboardDelegate property rather than the delegate property. leaderboardDelegate requires a <GKLeaderboardViewControllerDelegate> while delegate requires a <UINavigationControllerDelegate>, hence the error message.
Check the apple docs for leaderboardDelegate
https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLeaderboardViewController_Ref/#//apple_ref/occ/instp/GKLeaderboardViewController/leaderboardDelegate
I'm using google maps SDK 1.7.2 on a projectd using iOS 7 (I just upgraded it from iOS6). For some reason all the GMSMapViewDelegate callbacks work but this one
- (BOOL) didTapMyLocationButtonForMapView: (GMSMapView *)mapView
I'm assuming this should be called when the arrow button is tapped right? Any idea why it isn't?
This is how I instantiate the mapsview:
mapView_ = [GMSMapView mapWithFrame:[[self mainView] bounds]
camera:[self currentCameraUseStandartZoom:YES]];
[[mapView_ settings] setMyLocationButton:YES];
[mapView_ setDelegate:self];
[[self mainView] addSubview:mapView_];
I use a pretty reliable method to find the my location button.
Initially when a map view is created the button is hidden. And I go through map view hierarchy to find all hidden buttons. Then I set self.mapView.settings.myLocationButton = YES and check if any of the buttons I found is not hidden anymore.
Here is the code I use:
- (UIButton*)findAndShowMyLocationButton
{
NSMutableArray* hiddenButtons = [NSMutableArray array];
[self findHiddenButtonsInView:self.mapView hiddenButtons:hiddenButtons];
self.mapView.settings.myLocationButton = YES;
for (UIButton* button in hiddenButtons) {
if (!button.hidden) return button;
}
return nil;
}
- (void)findHiddenButtonsInView:(UIView*)view hiddenButtons:(NSMutableArray*)hiddenButtons
{
for (UIView* subview in view.subviews) {
if (subview.hidden && [subview isKindOfClass:[UIButton class]]) {
[hiddenButtons addObject:subview];
} else {
[self findHiddenButtonsInView:subview hiddenButtons:hiddenButtons];
}
}
}
And finally
- (void)viewDidLoad
{
...
UIButton* myLocationButton = [self findAndShowMyLocationButton];
[myLocationButton addTarget:self action:#selector(myLocationClick) forControlEvents:UIControlEventTouchUpInside];
}
just in case someone is having the same problem.. i pretty much resolved this by using a hack.. here it is:
in my main.m file I customized the UIResponder class:
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([TPMUApplication class]), NSStringFromClass([TPMUAppDelegate class]));
}
}
TPMUApplication.h
#import <UIKit/UIKit.h>
#class TPMUTaxiRequestVC;
#interface TPMUApplication : UIApplication
// basically the view controller that will be informed that
// the user has tapped the my location button, normally it would subscribe to
// the GMSMapViewDelegate protocol, and it should have a GMSMapView property
#property (nonatomic, strong) TPMUTaxiRequestVC *taxiRequestVC;
#end
TPMUApplication.m
#import "TPMUApplication.h"
#import "TPMUTaxiRequestVC.h"
#implementation TPMUApplication
- (void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
UIView *touchReceipientView =((UITouch *)[event.allTouches anyObject]).view;
NSLog(#"");
CGRect myLocationButtonFourchIncFrame = CGRectMake(256, 525, 64, 54);
CGRect myLocationButtonThreeHalfIncFrame = CGRectMake(256, 336, 64, 54);
if (CGRectEqualToRect(touchReceipientView.frame, myLocationButtonFourchIncFrame) ||
CGRectEqualToRect(touchReceipientView.frame, myLocationButtonThreeHalfIncFrame)) {
if (self.taxiRequestVC.mapState != TPMUMapStateInMotionAsResultOfMyLocationButtonTap) {
self.taxiRequestVC.mapState = TPMUMapStateInMotionAsResultOfMyLocationButtonTap;
// notice that didTapMyLocationButtonForMapView is actually
// a method in the GMSMapViewDelegate protocol.. and since
// taxiRequestVC subscribes to that protocol.. we simply call it here
// as if it was natively called
[self.taxiRequestVC didTapMyLocationButtonForMapView:self.taxiRequestVC.mapView];
}
}
}
#end
and just in case you were wondering, TPMUMapStateInMotionAsResultOfMyLocationButtonTap is a state of a state machine variable with the following states:
typedef enum
{
TPMUMapStateIdle = 0,
TPMUMapStateInMotionAsResultOfUserGesture,
TPMUMapStateInMotionAsResultOfMyLocationButtonTap
} TPMUMapState;
since I wanted to track motion in the map as a result of location button tap vs user gesture.
hope this helps!
I'm new to objective-c and iOS programming.I want to use a custom number pad for a game input. There are several types of games, (game1 game2 game3) and the games may require more or less numbers to be displayed on the number pad (control).
Each games rules and display properties are written in separate view controller classes. I tried cutting and pasting a copy of the number pad from game1 to the other controllers. It throws an error (at build), saying the name for the number buttons properties that I am using have already been used.
Will this would require me to rename and relink all object and properties for each game. To much maintenance overhead for me. So I have been trying to make a NumPad class, derived from NSObject.
This works fine until I try to create a view/container view from the non view controller class. I instantiate the numpad object in the viewdidload section of the game1 view controller. This is what I have so far.
.h file
#import <Foundation/Foundation.h>
#import "NumberPadTestAppDelegate.h"
#interface NumPad : NSObject
-(void)numberPadSetUp: (int) numberOfButtons;
#end
.m file
#import "NumPad.h"
#implementation NumPad
-(void)numberPadSetUp: (int) numberOfButtons
{
// Instantiate a Container View by code??? test.
CGRect frame = CGRectMake(14, 47, 740, 370);
UIView *Mycontainer = [[UIView alloc] initWithFrame:frame];
Mycontainer.backgroundColor = [UIColor blueColor];
[Mycontainer addSubview:Mycontainer];
// This was here for testing before adding the above four lines.
for (int a = 1; a < numberOfButtons +1; a++)
{
NSLog(#"Button %i has been added.",a);
}
NSLog(#" ");
NSLog(#"Numberpad setup is complete.");
} // End of "numberPadSetUp" routine.
#end
.m file from game1 test controller
#import "NumberPadTestViewController.h"
#interface NumberPadTestViewController ()
#end
#implementation NumberPadTestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NumPad *MyNumberPad =[[NumPad alloc]init];
[MyNumberPad numberPadSetUp:9];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This code gives a run time error as written.
int main(int argc, char * argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([NumberPadTestAppDelegate class]));
}
}
I was also wondering if using the container view control in IB would accomplish the task or might I get the same naming errors as before?
What am i missing here?
Problem is in this line
[Mycontainer addSubview:Mycontainer];
You are declaring Mycontainer and adding it to itself. You should write class like follow
#import "NumPad.h"
#implementation NumPad
#define kButtonWidth 50
#define kButtonHeight 15
#define kPadding 20
-(void)numberPadSetUp: (int) numberOfButtons
{
// Instantiate a Container View by code??? test.
// This was here for testing before adding the above four lines.
int xPos = 0;
int yPos = 0;
for (int a = 1; a < numberOfButtons +1; a++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];
CGRect aButtonFrame = CGRectMake(xPos, yPos, kButtonWidth, kButtonHeight);
aButton.frame = aButtonFrame;
[aButton setTitle:[NSString stringWithFormat:#"%d",a] forState:UIControlStateNormal];
[self addSubview:aButton];
xPos+=kButtonWidth+kPadding;
if(fmod(a, 3)==0){
yPos+=kButtonHeight+kPadding;
xPos = 0;
}
NSLog(#"Button %i has been added.",a);
}
NSLog(#" ");
NSLog(#"Numberpad setup is complete.");
} // End of "numberPadSetUp" routine.
#end
And in your main class
NumPad *MyNumberPad =[[NumPad alloc]initWithFrame:CGRectMake(14, 47, 740, 370)];
[MyNumberPad numberPadSetUp:9];
[self.view addSubview:MyNumberPad];
[self.view bringSubviewToFront:MyNumberPad];
This will give you result like following images in iOS6/iOS7
I tried to use Cleaver, that is PhoneGap / Cordova as a component of an existing iOS application. I followed this step by step guide twice, without success.
My application can easily instantiate a web view, and i can pass content to it using the stringbyevaluatingjavascriptfromstring method, but as soon as i try to access PhoneGap API (navigator.compass, navigator.network, ...), nothing seems to work. console.log(navigator) shows no sign of Cordova objects (console.log does not output anything is Xcode, i'm using http://debug.phonegap.com/). The deviceready event is not triggered either.
In my html file, i include PhoneGap js file cordova-1.7.0rc1.js which i copied from another project (since /www folder is missing when you use PhoneGap as a component).
My ViewController.h imports <Cordova/CDVViewController.h>.
Here my ViewController.m
//
// ViewController.m
// CordovaComponent
//
#import "ViewController.h"
#interface ViewController (){
CDVViewController *cdvViewController ;
}
#end
#implementation ViewController
- (void)pushPhoneGap:(id)sender {
cdvViewController = [[CDVViewController alloc] init];
cdvViewController.wwwFolderName = #"www";
cdvViewController.startPage = #"index.html";
cdvViewController.view.frame = self.view.bounds;
cdvViewController.webView.delegate = self;
[self.navigationController pushViewController:cdvViewController animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Push PhoneGap view" forState:UIControlStateNormal];
[button addTarget:self action:#selector(pushPhoneGap:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(60, 50, 200., 50.);
[self.view addSubview:button];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *jsReturn = [cdvViewController.webView stringByEvaluatingJavaScriptFromString:#"setArray([1, 1, 2, 3, 5, 8, 13, 21, 34, 1]);"];
NSLog(jsReturn);
}
#end
Do you have an idea of what's happening ? Any help would be greatly appreciated !
Okay, after many hours of trying to solve this issue, I finally found what was going wrong.
This is the line that messed things up:
cdvViewController.webView.delegate = self;
You can't just override the delegate of the view controllers's web view. The original delegate has instantiation code which enables PhoneGap to run. Don't override it and everything will be fine !
If you want to get notified about webview events without breaking Cordova you could try something like this:
#interface WrappingWebViewDelegate : NSObject <UIWebViewDelegate>
#property (nonatomic,retain) id<UIWebViewDelegate> wrappedDelegate;
#end
#implementation WrappingWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView*)theWebView {
[self.wrappedDelegate webViewDidStartLoad: theWebView];
// your code
}
// implement all other delegate methods and forward them to the wrapped delegate
[...]
#end
Usage like this:
cdvViewController = [[CDVViewController alloc] init];
WrappingWebViewDelegate wrappingDelegate = [[WrappingWebViewDelegate alloc] init];
wrappingDelegate.wrappedDelegate = cdvViewController.webView.delegate;
cdvViewController.webView.delegate = wrappingDelegate;
I haven't tested this, so you should be careful.