I am making a game in Sprite Kit and I am wondering if it is possible to make a SKAction which changes he gravity for a while, is it?
This is the code I already have:
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:#"bal.png"];
sprite.position = CGPointMake(self.frame.size.width/4 + arc4random() % ((int)self.frame.size.width/2), (self.frame.size.height/2 + arc4random() % ((int)self.frame.size.height/2)));
sprite.color = [self randomColor];
sprite.colorBlendFactor = 1.0;
sprite.xScale = 0.2;
sprite.yScale = 0.2;
[self addChild:sprite];
sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width/2];
self.physicsWorld.gravity = CGVectorMake(0.0f, -4.0f);
Thanks in advance!
you run custom action blocks and turn it on and off, let me know if this works for you.
[self runAction:[SKAction sequence:#[[SKAction runBlock:^{ self.physicsWorld.gravity = CGVectorMake(0.0f, -4.0f);}],[SKAction waitForDuration:4],[SKAction runBlock:^{ self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);}]]]];
Related
I am looking to shoot a SKSpritenode (a cannonball) from a another Sprite node (enemy ship).
The cannonball should travel directly down toward bottom of screen from the enemy ship node.
I can't seem to get positioning right, cannonball seems to shoot from the corner of the screen and not move far, the enemy-ship node is randomly chosen from an array of all halos on screen at once and the cannonball position assigned to the front of halo:
// the cannonball
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:6];
ball.physicsBody.velocity = CGVectorMake(SHOT_SPEED, SHOT_SPEED);
// we dont want ball to lose speed or momentum when it hots edges so..
ball.physicsBody.restitution=1.0; // max bounceness of node
ball.physicsBody.linearDamping =0.0; // dont reduce linear velocity ove time
ball.physicsBody.friction=0.0;
ball.physicsBody.categoryBitMask = kCCBallCategory;
// ball.physicsBody.contactTestBitMask = kCCEdgeCategory;
ball.physicsBody.collisionBitMask= kCCEdgeCategory;
ball.physicsBody.contactTestBitMask = kCCEdgeCategory | KCCShieldUPCategory | kCCMultiUpCategory ; // | KCCShieldUPCategory notify
// the array of enemy ship nodes
NSMutableArray *allHalos = [NSMutableArray array];
[_mainLayer enumerateChildNodesWithName:#"halos" usingBlock:^(SKNode *node, BOOL *stop) {
[allHalos addObject:node];
}];
if ([allHalos count]>0) {
NSUInteger allHalosInteger = arc4random_uniform([allHalos count]);
SKSpriteNode *shooterHalo=[allHalos objectAtIndex:allHalosInteger];
ball.position = CGPointMake(shooterHalo.position.x, shooterHalo.position.y - shooterHalo.frame.size.height/2 + ball.frame.size.height / 2);
CGPoint bulletDestination = CGPointMake(shooterHalo.position.x, - ball.frame.size.height / 2);
[self fireBullet:ball toDestination:bulletDestination withDuration:2.0 ];
}
-(void)fireBullet:(SKNode*)ball toDestination:(CGPoint)destination withDuration:(NSTimeInterval)duration {
//1
SKAction* bulletAction = [SKAction sequence:#[[SKAction moveTo:destination duration:duration],
[SKAction waitForDuration:3.0/60.0],
[SKAction removeFromParent]]];
[ball runAction:[SKAction group:#[bulletAction, _soundLaser]]];
[self addChild:ball];
}
Any input appreciated.
I have a button performs some action, have added uiwebview as a sub view of the button to load a small gif animation, I can tap the button to perform the task but I needed to perform the same action when I tap the web view.
-(void)numeritsIphone4{
arrayButtonNames = #[#"Button1.png",#"Button2",#"Button3",#"Button4",#"Button5.png",#"Button6.png",#"Button7.png",#"Button8.png",#"Button9.png",#"Button10.png"];
float y = 20;
int x = 3;
int count = 0;
for (int i = 0 ; i < 10; i++)
{
count ++;
buttonsarray[i] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonsarray[i].frame = CGRectMake(x, y, 155, 90);
buttonsarray[i].tag = i;
[buttonsarray[i] setBackgroundImage:[UIImage imageNamed:[arrayButtonNames objectAtIndex:i]] forState:UIControlStateNormal];
[buttonsarray[i].titleLabel setFont:[UIFont fontWithName:#"Verdana" size:80]];
[buttonsarray[i] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//buttonsarray[i].titleLabel.textAlignment = UITextAlignmentLeft;
buttonsarray[i].contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[buttonsarray[i] setTitle:[NSString stringWithFormat:#"%d",digit] forState:UIControlStateNormal];
digit ++;
character = [[UIWebView alloc]initWithFrame:CGRectMake(100, 30, 25, 25)];
NSURL *url=[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Star" ofType:#"gif"]];
character.opaque = NO;
character.backgroundColor = [UIColor clearColor];
character.scrollView.scrollEnabled = NO;
[character loadRequest:[NSURLRequest requestWithURL:url]];
character.userInteractionEnabled = YES;
[buttonsarray[0] addSubview:character];
[character release];
[url release];
[buttonsarray[i] addTarget:self action:#selector(performOperation4:) forControlEvents:UIControlEventTouchUpInside];
x = x + 159;
[self.view addSubview:buttonsarray[i]];
if(count == 2)
{
count = 0;
x = 3;
y = y+ 92;
}
}
A possibility is to add a UITapGestureRecognizer to your UIWebView control and call the same function when the gesture recognizes.
NOTE: This way you have limitation if you have some buttons, links etc on your UIWebView page, that button, links might not get trigger. So give it a try and see if it helps you.
I am working on a game that incorporates touch detection with skShapenodes that are circles, and can not find a good method to check if they are touching. Code for the player class is below
-(void)SpawnPlayer
{
_player = [[SKShapeNode alloc] init];
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 15, 0, M_PI*2, YES);
_player.path = myPath;
_player.lineWidth = 1.0;
_player.fillColor = [SKColor whiteColor];
_player.strokeColor = [SKColor whiteColor];
_player.glowWidth = 0.5;
_location = CGPointMake(375, 400);
_player.position = CGPointMake(375, 400);
_player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:0.5];
_player.physicsBody.dynamic = NO;
_player.physicsBody.categoryBitMask = playerCategory;
_player.physicsBody.contactTestBitMask = enemyCategory;
_player.physicsBody.collisionBitMask = 0;
[self addChild:_player];
}
The enemy code is similar, with the exception that its bitmask and testbitmask are switched.
Think of it as this:
The categoryBitMask specifies the type of body a node is.
The collisionBitMask specifies which type of bodies it can collide with.
The contactTestBitMask specifies which type of contactTestBitMasks will interact with it.
e.g. to detect contact between player and enemy, which have different categoryBitMasks, their contactTestBitMasks should be:
_player.physicsBody.categoryBitMask = playerCategory;
_player.physicsBody.contactTestBitMask = playerCategory | enemyCategory ;
For enemy:
_enemy.physicsBody.categoryBitMask = enemyCategory;
_enemy.physicsBody.contactTestBitMask = playerCategory | enemyCategory ;
now you can handle what to do on contact in didBeginContact method
I have 6 sprite images I am trying to add to my scene, adding each of them seems to slow everything down a lot. I figured I would need to create an NSArray in order to help with speed. Here is the array I've created, but it's only adding the first image, how can I get it to add all 6?? Thank you in advance!
myArray
NSArray *myArray = [NSArray arrayWithObjects:#"image1",#"image2",#"image3",#"image4",#"image5",#"image6", nil];
NSInteger count = [myArray count];
for (int i = 0; i < count; i++) {
if (i > 5) {
break;
}
result = [myArray objectAtIndex:i];
}
//Setting SKSpriteNodes from array.
dice = [SKSpriteNode spriteNodeWithImageNamed:[myArray objectAtIndex:result.intValue]];
Define a property in you scene:
#interface MyScene
#property (nonatomic) NSMutableArray *items;
#end
Then create a method to fill that array:
- (void)fillItems {
for (int i=0; i<10; i++) {
SKSpriteNode *d1 = [SKSpriteNode spriteNodeWithImageNamed:#"Sprite1"];
d1.position = CGPointMake(self.frame.size.width/4 + arc4random() % ((int)self.frame.size.width/2),
self.frame.size.height/2 + arc4random() % ((int)self.frame.size.height/2));
d1.color = [self randomColor];
d1.colorBlendFactor = 1.0;
d1.xScale = 0.25;
d1.yScale = 0.25;
d1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:d1.frame.size];
//Adding SpriteKit physicsBody for collision detection
d1.physicsBody.categoryBitMask = diceCategory;
d1.physicsBody.dynamic = YES;
d1.physicsBody.contactTestBitMask = frameCategory;
d1.physicsBody.collisionBitMask = diceCategory | frameCategory;
d1.physicsBody.usesPreciseCollisionDetection = YES;
d1.name = #"Sprite1";
[self.items addObject:d1];
[self addChild:d1];
}
ı have a problem about scrollview.
my project about a newspaper.my problem about : I want to start at the
top of the page when changing page.
scrollview have 8 news.ı want to beginning at the top of the text on
the page when change 2. or 3. or.. news..
please help me about it.because ı need to deliver the project on Monday.for this reason ı
must do today or tomorrow.
code of project in here
myScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
myScrollview.pagingEnabled = YES;
myScrollview.scrollEnabled =YES;
myScrollview.clipsToBounds = NO;
myScrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollview.showsHorizontalScrollIndicator = YES;
myScrollview.backgroundColor = [UIColor blackColor];
myScrollview.delegate = self;
NSInteger viewcount=4;
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"photo1.png"],[UIImage imageNamed:#"photo2.png"],[UIImage imageNamed:#"photo3.png"],[UIImage imageNamed:#"photo4.png"],nil];
for (int i = 0; i <viewcount; i++)
{
CGFloat x = i * self.view.frame.size.width;
subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];
[subView setBackgroundColor:[UIColor blackColor]];
[subView setCanCancelContentTouches:NO];
subView.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview
subView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
aImageView = [[UIImageView alloc ] initWithImage:[images objectAtIndex:i]];
aImageView.frame=CGRectMake(0, 0, 320, 900);
[aImageView setTag:viewcount];
[subView addSubview:aImageView];
[subView setContentSize:CGSizeMake(aImageView.frame.size.width, subView.frame.size.height)];
subView.minimumZoomScale = 1;
subView.maximumZoomScale = 9;
subView.delegate = self;
[subView setScrollEnabled:YES];
subView.contentSize = aImageView.frame.size;
[myScrollview addSubview:subView];
}
myScrollview.contentSize = CGSizeMake(self.view.frame.size.width*viewcount,self.view.frame.size.height);
[self.view addSubview:myScrollview];
https://biliyomusunuz.com