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

  • SEARCH
  • Home
  • 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 7870755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:42:54+00:00 2026-06-03T01:42:54+00:00

I used the tile tutorial from www.raywenderlich.com. I set up a tile map in

  • 0

I used the tile tutorial from http://www.raywenderlich.com. I set up a tile map in a landscape view and used the code from the tutorial to detect touch, like this:

    CGPoint touchLocation = [touch locationInView: [touch view]];       
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];

And use this bit to figure which tile it is on:

    CGPoint touchTilePos = [self tileCoordForPosition:touchLocation];
    int tileIndex = [self tileIndexForTileCoord:(touchTilePos)];
    CCSprite *touchTile = [self.background tileAt:touchTilePos];
    self.player.position = [touchTile.parent convertToWorldSpace:touchTile.position];

Problem is that it’s a bit off. Touch close to the left side is fairly close, but touch close to the right side is detected way off to the left. Seems like some sort of scaling issue, but the only scaling in my code is the player sprite.

Any ideas? Suggestions?
Any halp is greatly appreciated!

EDIT: here is the two methods referenced in the code:

- (CGPoint) tileCoordForPosition:(CGPoint)position 
{
    int x = position.x / _tileMap.tileSize.width;
    int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - position.y) / _tileMap.tileSize.height;
    return ccp(x, y);  
}

- (int) tileIndexForTileCoord:(CGPoint)aTileCoord
{
    return aTileCoord.y*(self.tileMap.mapSize.width) + aTileCoord.x;
}

UPDATE:
I simplified the code:

CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self.background convertToNodeSpace:touchLocation];

int x = touchLocation.x / _tileMap.tileSize.width;
int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - touchLocation.y) / _tileMap.tileSize.height;
CGPoint touchTilePos = ccp(x,y);

Also, i noticed that right off the start, touchLocation is off to the left, i don’t think this code works correctly for my case (iPad/landscape):

CGPoint touchLocation = [touch locationInView: [touch view]];
  • 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-06-03T01:42:55+00:00Added an answer on June 3, 2026 at 1:42 am

    I made a fatal mistake! I am using HEX tiles, and they overlap a bit (about 1/4). I have to account for that! And that’s why the effect gets worse as i tap further to the right,
    Here is the code to fix it:

        int x = (touchLocation.x - tileSize.width/8) / (_tileMap.tileSize.width * (3.0/4.0)) ;
        int y = 0;
        if(x % 2 == 0)
        {
            // even
            y = ((_tileMap.mapSize.height * tileSize.height) - touchLocation.y) / tileSize.height;
        }
        else
        {
            // odd
            y = ((_tileMap.mapSize.height * tileSize.height) - (touchLocation.y + tileSize.height/2)) / tileSize.height;
        }
        CGPoint touchTilePos = ccp(x,y);
    

    Cheers!

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

Sidebar

Related Questions

Used code I found on SO to use the COM based Acrobat Reader to
Whatever screen is being used to view the page, I would like to show
Here is the code: https://www.dropbox.com/s/o42wy36x4qhrbpt/PDFScroller.zip I took the WWDC 2010 PhotoScroller sample code that
I used DataSet to load a schema from following xml file; <node id=0> <node
Is the dock tile plug-in of an application used even when the application is
I have been reading through several websites and even used NVIDA's code as a
I used the code below to create a graphic using dot (graphviz). digraph {
Having the following code, how can I have get receive a map, and return
I'm trying to draw a tile map in C# and the problem I'm having
I'm not sure why this is happening. Basically I render my tile map to

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.