i've got a column of sprites as one sprite goes off the screen i want the same sprite to wrap around the opposite side so that two sprites are showing simultaneously, as one moves of the screen the other comes onto the screen and the other sprite that goes off is no longer visible. I was told to make a ccnode in which to do everything here is my code so far, but none of it works so i think i will need to start from scratch again.
Here is a link to my last question on this for more info: When sliding sprite, if sprite disappears off the side, it will wrap around to the opposite side?
here is my code anyways:
for (int i =0; i<16; ++i) {
MyNode *currentSprite = [c1array objectAtIndex:i];
if (currentSprite.contentSize.height>=320 || currentSprite.position.y-currentSprite.contentSize.height/2<=0 ){
MyNode *Bsprite = currentSprite;
MyNode *Tsprite = currentSprite;
Bsprite.scale = 1.0;
Tsprite.scale = 1.0;
if(currentSprite.position.y >=253){
Bsprite.position = ccp(currentSprite.position.x,-35);
[self addChild:Bsprite];
Bsprite.visible = TRUE;
}
if (currentSprite.position.y <=0) {
Tsprite.position = ccp(currentSprite.position.x,324);
[self addChild:Tsprite];
Tsprite.visible = TRUE;
}
MyNode *isChanging;
if ((Tsprite.visible == TRUE && currentSprite.visible == TRUE) || (Bsprite.visible == TRUE && currentSprite.visible == TRUE)) {
isChanging = TRUE;
}
if (isChanging == FALSE) {
[self removeChild:Tsprite cleanup:YES];
[self removeChild:Bsprite cleanup:YES];
}
}
}
BSprite and TSprite are pointers to the same object (currentSprite). You actually need two separate objects, either by cloning currentSprite or by creating another array in the same manner as c1array.
Related
Edit: Hi at all! (forgot to say hello...)
I got a pretty annoying problem.
I've created a small program which works quite good so far. However, I'm trying to change an SKSpriteNode texture whenever it's hovering over another SKSpriteNode.
To test the implementation I added an NSLog() to print the hovered nodes origin.
THAT works. But the texture change only works when hovering over the last itereated node.
Complicated. Here is some code:
-(void)checkAimingHover{
for (SKSpriteNode *childSprite in [parentNode children]) {
CGRect boundingBox =
CGRectMake(childSprite.position.x-childSprite.size.width/2,
childSprite.position.y-childSprite.size.height/2,
childSprite.frame.size.width,
childSprite.frame.size.height);
if (CGRectContainsPoint(boundingBox, aiming.position)) {
// THIS works like a charm
NSLog(#"%f/%f",boundingBox.origin.x, boundingBox.origin.y);
// THIS only works on the last created boundingBox
[aiming setTexture:[SKTexture textureWithImageNamed:#"aim_hover"]];
} else [aiming setTexture:aimDefault];
}
}
aming: SKSpriteNode *aiming;
And the small update():
-(void)update:(NSTimeInterval)currentTime {
if (lastUpdateTime) {
deltaTime = currentTime - lastUpdateTime;
} else {
deltaTime = 0;
}
lastUpdateTime = currentTime;
[self checkAimingHover];
}
You are resetting your texture on each loop. Try this instead:
BOOL hitTestDidHit = NO;
for (SKSpriteNode *childSprite in [parentNode children]) {
CGRect boundingBox =
CGRectMake(childSprite.position.x-childSprite.size.width/2,
childSprite.position.y-childSprite.size.height/2,
childSprite.frame.size.width,
childSprite.frame.size.height);
if (CGRectContainsPoint(boundingBox, aiming.position)) {
// THIS works like a charm
NSLog(#"%f/%f",boundingBox.origin.x, boundingBox.origin.y);
// THIS only works on the last created boundingBox
hitTestDidHit = YES;
}
}
if(hitTestDidHit) {
[aiming setTexture:[SKTexture textureWithImageNamed:#"aim_hover"]];
} else {
[aiming setTexture:aimDefault];
}
Also I should tell you about the super cool .calculateAccumulatedFrame property which pretty much does what your bounding box does but with children as well.
I am using SpriteKit.
The code below basically makes a lattice of dots on the screen. However, I want to call each 'dot' a different name based on its position, so that I can access each dot individually in another method. I'm struggling a little on this, so would really appreciate if someone could point me in the right direction.
#define kRowCount 8
#define kColCount 6
#define kDotGridSpacing CGSizeMake (50,-50)
#import "BBMyScene.h"
#implementation BBMyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
// Background
self.backgroundColor = [SKColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1]; /*#f4f4f4*/
CGPoint baseOrigin = CGPointMake(35, 385);
for (NSUInteger row = 0; row < kRowCount; ++row) {
CGPoint dotPosition = CGPointMake(baseOrigin.x, row * (kDotGridSpacing.height) + baseOrigin.y);
for (NSUInteger col = 0; col < kColCount; ++col) {
SKSpriteNode *dot = [SKSpriteNode spriteNodeWithImageNamed:#"dot"];
dot.position = dotPosition;
[self addChild:dot];
//6
dotPosition.x += kDotGridSpacing.width;
}
}
}
return self;
}
Here is an image of what appears on screen when I run the above code...
http://cl.ly/image/3q2j3E0p1S1h/Image1.jpg
I simply want to be able to call an individual dot to do something when there is some form of user interaction, and I'm not sure how I would do that without each dot having a different name.
If anyone could help I would really appreciate it.
Thanks,
Ben
- (void)update:(NSTimeInterval)currentTime {
for(SKNode *node in self.children){
if ([node.name containsString:#"sampleNodeName"]) {
[node removeFromParent];
}
}
}
Hope this one helps!
You can set the name property of each node inside the loop.
Then you can access them with self.children[index].
If you want to find a specific node in your children, you have to enumerate through the array.
Update:
To clarify how to search for an item by iterating, here is a helper method:
- (SKNode *)findNodeNamed:(NSString *)nodeName
{
SKNode *nodeToFind = nil;
for(SKNode *node in self.children){
if([node.name isEqualToString:nodeName]){
nodeToFind = node;
break;
}
}];
return nodeToFind;
}
I'm just starting to wrap my brain around Sprite Kit and I am encountering a very strange error when attempting to change the property of a node in a for loop im using.
I have two SKSpriteNode objects, one is the child of a SKScene (BLATheBugs) and the other is a child of the first (BLAEmptySpaces). I have a grid laid out with BLAEmptySpaces, and BLATheBugs on top of those empty spaces which are supposed to take UITouch, and move to an empty space if its bool isOccpupied property == False. When the scene is set up, the SKScene triggers a method in TheBugs:
-(void) spawnEmptySpacesInitialize
{
[self addChild:[self spawnEmptySpaces]];
}
which in turn triggers:
-(BLAEmptySpaces *) spawnEmptySpaces
{
emptySpace = [[BLAEmptySpaces alloc] init];
emptySpace.numberOfEmptySpacesNeeded = 12;
[emptySpace spawnEmptySpaces];
[emptySpace positionTheEmptySpaces];
return emptySpace;
}
which finally triggers a method in the EmptySpaces object:
-(BLAEmptySpaces *) spawnEmptySpaces
{
_emptySpacesArray = [NSMutableArray new];
for (int x = 0; x < _numberOfEmptySpacesNeeded; x++)
{
_anEmptySpace = [[BLAEmptySpaces alloc] initWithImageNamed:#"BlueLight.png"];
_anEmptySpace.zPosition = 50;
[_emptySpacesArray addObject:_anEmptySpace];
[self addChild: _anEmptySpace];
}
return self;
}
everything seems fine, (except for needing the additional "addChild" in the EmptySpaces object to get them to be drawn on the screen which i have also been trying to fix) but when i call the method to move TheBugs:
-(void) moveLeftOneSpace
{
NSLog(#"%d", emptySpace.emptySpacesArray.count);
for (emptySpace in emptySpace.emptySpacesArray)
{
NSLog(#"cycle");
if (emptySpace.isOccupied == NO)
{
for (_yellowBug in yellowBugArray)
{
if (_positionOfFingerTouchX > _yellowBug.position.x - variableOne && _positionOfFingerTouchX < _yellowBug.position.x + variableTwo && _positionOfFingerTouchY > _yellowBug.position.y - variableOne && _positionOfFingerTouchY < _yellowBug.position.y + variableTwo && emptySpace.position.x == _yellowBug.position.x - 80 && emptySpace.position.y == _yellowBug.position.y)
{
_yellowBug.position = CGPointMake(_yellowBug.position.x - spaceBetweenBugs, _yellowBug.position.y);
emptySpace.isOccupied = YES;
NSLog(#"execute");
}
}
}
}
}
It at first tells me there are 12 objects in the array and runs the operation. if I try to move any piece again, it tells me there are now NO objects in the array (yellowBugArray). It is also probably worth noting that it will not let me access emptySpace.anEmptySpace. Throws me an error.
Sorry for the long post, but hopefully somewhere in here is the cause of my problem.
Thank you very much guys!
I´m making an iphone app in objective-c with cocos2d, in the code below I try to detect a collision and then run an animation. (The box1 is moved by touch)
When the "[self getChildByTag:d]" and "box1" collide AND overlap I get the "JUMP NOW!" displayed but I don't get the jump itself, but when the box1 is moved away from the "[self getChildByTag:d]" the jump occurs.
I understand that this probably has to do with the fact that the action is called many times, but please explain to me exactly what happens and please help me with a solution!
- (void)update:(ccTime)dt {
for (int d = lowestAvailableTag; d <= highestAvailableTag; d++) {
if ([self getChildByTag:d].position.y < (box1.position.y+45)&&
[self getChildByTag:d].position.x > (box1.position.x-45) &&
[self getChildByTag:d].position.x < (box1.position.x+45) ) {
NSLog(#"JUMP NOW!");
if ([self getChildByTag:d].position.x < 150) {
[[self getChildByTag:d] runAction:
[CCJumpTo actionWithDuration:1.5
position:ccp(240, 140) height:110 jumps:1]];
}
}
}
}
//albar
You can add some BOOL flag to detect if your jump occured. Smth like:
- (void) update:(ccTime)dt
{
if( jumpOccured == false )
{
BOOL needToJump = // your jump condition
if( needToJump == true )
{
// your jump code
jumpOccured = true;
}
}
}
by the way, if you have many possible collisions, you can use box2d to detect them
I have been looking for weeks for an OBJ-C based tutorial on how to call a method when a specific b2body collides with something else (not everything).
Basically, a block falls to the ground every second. This works fine, but when it hits the ground or the player, the block should get deleted and pieces of it (different object) should be spawned.
Can anyone tell me how to do this?
Any help would be hot
Thanks
I think this is the one that you need..You need to include MycontactListener
-(void) checkForHit{
std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
bodyA = contact.fixtureA->GetBody();
bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
spriteA = (CCSprite *) bodyA->GetUserData();
spriteB = (CCSprite *) bodyB->GetUserData();
//NSLog(#”sprite tag is %d”,spriteA.tag);
if (spriteA.tag == 50) {
if (std::find(toDestroy.begin(), toDestroy.end(), bodyB) == toDestroy.end()) {
toDestroy.push_back(bodyB);
}
}
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) body->GetUserData();
[self removeChild:sprite cleanup:YES];
}
_world->DestroyBody(body);
}
or if you dont't want to use contact listener.you can create a fixture for ground body and a body that u want to destroy and use the following code to check if it is intersecting and destroy it...
if((contact.fixtureA == groundFixture && contact.fixtureB == bodyFixture) ||
(contact.fixtureA == bodyFixture&& contact.fixtureB == groundFixture ))
{
//destroy the body
}
I have realized that as my object fall to the ground, i could create my own VERY simple physics by creating a start velocity, and then increasing it in the update method by a specific amount.
For the collision, my objects all fit in a 48pt wide grid so i could do simple rectangular collision detection.