Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4584002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:17:38+00:00 2026-05-21T21:17:38+00:00

I have an sprite the I need to rotate it with touch but it

  • 0

I have an sprite the I need to rotate it with touch but it is located in a different layer. Is it possible to update it’s position?

E.G.
Sprite has it’s own layer but it’s position needs to be updated within the main gamescene

here is what I have so far.

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "GameScene.h"



@interface G : CCLayer {

    CCSprite *g;

    CGFloat gRotation;
}

@end

------------------------------------------
#import "G.h"


@implementation G

-(id) init
{
    if ((self = [super init]))
    {
        CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);

        g = [CCSprite spriteWithFile:@"g.png"];

        [self addChild:g z:-1];

        //[self scheduleUpdate];
        if (g.rotation == 360)
        {
            [self unscheduleUpdate];
        }
    }
    return self;
}

- (void)update:(ccTime)delta
{
    g.rotation = gRotation;
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint firstLocation = [touch previousLocationInView:[touch view]];
    CGPoint location = [touch locationInView:[touch view]];

    CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
    CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];

    CGPoint firstVector = ccpSub(firstTouchingPoint, g.position);
    CGFloat firstRotateAngle = -ccpToAngle(firstVector);
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);

    CGPoint vector = ccpSub(touchingPoint, g.position);
    CGFloat rotateAngle = -ccpToAngle(vector);
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);

    gRotation += currentTouch - previousTouch;
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void) dealloc
{
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);

    [super dealloc];
}

@end

GameScene

#import "GameScene.h"
#import "MainMenu.h"
#import "G.h"


@implementation GameScene

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    GameScene *layer = [GameScene node];

    [scene addChild: layer];
    return scene;
}

-(void) tapG: (id) sender
{

    G *gView;
    gView = [[G alloc] init];
    gView.position = ccp(100, 100);

    [self.parent addChild:gView z:1001];

    [gView schedule:@selector(update:)];

    [gView release];
}
-(id) init
{
    if ((self = [super init]))
    {
tG = [CCMenuItemImage itemFromNormalImage:@"tp.png" selectedImage:@"tp.png"  disabledImage:@"tpaperd.png" target:self selector:@selector(tapG:)];

        gt = [CCMenu menuWithItems:tG, nil];
        gt.position = ccp(210, 80);
        [gt alignItemsHorizontallyWithPadding:10];

        [self addChild:gt z:0];

    }
    return self;
}
- (void) dealloc
    {
        CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);

        [super dealloc];
    }

It brings up the sprite but the sprite does not rotate.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:17:39+00:00Added an answer on May 21, 2026 at 9:17 pm

    you can schedule updates for another layer.

    are you sure your G layer is set to be touch enabled? try gView.isTouchEnabled = YES; after allocating gView. you may need to confirm to the protocol. Add <CCStandardTouchDelegate> to your CCLayer interface: @interface G : CCLayer <CCStandardTouchDelegate> { ....

    also you can schedule -(void)update:(ccTime)delta method using [gView scheduleUpdate]; it will give the same result but its more convenient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to rotate a 2d sprite about the y axis. E.g., I have
I have a 2D animation in Javascript, and I need a sprite to move
I have a sprite in scene1.m and I have a button in HUD layer
I have a sprite loaded from a PNG that has an alpha channel. The
I have a sprite and if it is touched the touch should be recognized.
I have a sprite(image of a ball) I can move it using touch and
I have tried to use different methods but to no avail. So I come
I need to rotate a character sprite according to the scope of the platform
This is probably a newbie question, but is it possible to add a sprite
I have a sprite loaded as a texture and I need to animate it,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.