cocos2d::Menu click detection is a bit off to bottom left whenever using a custom window size - cocos2d-x-3.0

After weeks of debugging, I struggle to figure out what was going on with cocos2d::Menu. I was surprise that my MenuItemImage was not receiving callbacks when I click on the button. So I click anywhere else out of frustration and boom, as I clicked on the bottom left corner, the callback registers in!
Its a bit off towards the bottom-left and wondering what might caused this? I already reproduce the problem using the crappy sample project provided as is and was not able to reproduce this using the default window size. But the problem was now reproducible when I change to a custom size. I also notice that the detection bounds seems to be not the actual shape of the given MenuItemImage. Its a bit square.
I am quite on predicament here. This is really frustrating. Documentation and the community is as unhelpful as hell.
I am using cocos2d-x v3.6;
Here is my code:
mRetryButton = cocos2d::MenuItemImage::create("asset_button_up.png", "asset_button_down.png", CC_CALLBACK_1(MainGameScene::onRetryButtonClicked, this));
mRetryButton->setPosition(cocos2d::Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f));
auto menu = cocos2d::Menu::create(mRetryButton, NULL);
menu->setPosition(cocos2d::Vec2::ZERO);
this->addChild(menu, 1);
Do you know any solution to this? Or did I just horribly missed something? I just needed a simple on-screen button. Nothing fancy. Just plain-old dull button.
Thanks!

I fixed it!
I tried numerous ways on how to create a button. All of them exhibited similar behavior.
There must be something wrong with the changing of the screenSize. I committed a terrible mistake.
On AppDelegate::applicationDidFinishLaunching when it first initialize the glview, I was once told that in order to change the screen size I need to call this: cocos2d::Director::getInstance()->getOpenGLView()->setFrameSize(width , height). I set this after this particular line such that it will look like this:
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::create("My Game");
director->setOpenGLView(glview);
}
director->getOpenGLView()->setFrameSize(width , height);
This is so wrong. Doing this after the director set the OpenGL view registers the original 960 , 640 screen size that reverberates all through out the scenes, layers, nodes you have. Every time you check on the debugger the content size of the node in question (The button I have has a size of (140 , 40)), it defaults to the original screen size. This is not right. There is something wrong there. AND this will not be fixed if you set the content size manually yourself. I think by incorrectly setting the screensize some nodes' content size become misaligned or not properly computed by the time the director set the GL view. The proper way of changing screen is this:
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::create("My Game");
glview->setFrameSize(370, 480);
director->setOpenGLView(glview);
}
Now, the click detection bounds, position and size, exactly fits onto the sprite image and can now receive callbacks! For *(#$*ing two weeks. I got it.
I hope this helps someone in the future!

Related

Rotation and constraints in swift3 (xcode)

I am using swift3 in xcode.
I have made an application with 10 sliders which I rotate to vertical using:
slider1outlet.transform = CGAffineTransform.init( rotationAngle: CGFloat(-Double.pi / 2))
Same goes for slider 2 through 10 this works without a problem,
but I want to add constraints so they go maximum height and are evenly distributed across the width of the screen, and they adapt when I rotate my screen.
This doesn't seem to work, the constraint seems to turn with the slider and I don't want that.
I have made screenshots (with only 3 sliders just for demo purposes)
the first slider has a red background to show what I mean, here is the screenshot of the application without rotating the sliders:
With the rotating sliders (only 1st slider has red background)
I had exactly the same problem, and it took me forever to figure out. I finally ended up writing my own class exactly for this: VerticalSlider.
Simply make new UISliders in your storyboard and set their classes to VerticalSlider.
It took me countless hours of trial and error in one of my projects to make that, so I'm glad I can finally help somebody else :D

Image aspect ratio swift ios

So, I'm learning iOS Swift. I'm going by some tutorials, and I'm stucked at image positioning.
I'm trying to figure out how uploaded picture is rescaled and positioned.Since I cannot post the screenshot, image that should be shown in my simulator as whole, I can only see like 25% of the picture. Should I change something in Attributes or in Size Inspector?
What I did so far (clearly wrong), was setting Intrinsic Size field -> select Placeholder, w/h = 320.
Then, I pinned and selected Aspect Ratio.
Any help, please?
Thanks.
If you're asking how to make sure an image fills a UIImageView:
myImageView.contentMode = UIViewContentMode.ScaleAspectFill
You may also need to set constraints if you placed your image view in interface builder.
It's not entirely clear from your question how you want it positioned, but hopefully these get you on the right path.

objective-c iphone sdk dragging image inside a box

I was thinking of make a slider/joystick but not in a circle but in a line. So I made the image drag able only over the X axis and when it gets to the point where the slider stops i did this :
if(slider.center.x <= 60) {
slider.center = CGPointMake(60, slider.center.y);
}
so it wouldn't get bigger than 60 but i was thinking isn't there a better way to do this? like make a box and setting up something to not drag it outside that box?
Thanks!
I think this method is very good. As far as I know there is no wan to stop dragging in other ways than this.
However there is a control called UISlider. This control does the functionality you would like to have.
You could also use touchesBegan, touchesMoved, and touchesEnded, then add an image behind and detect when touch is less than half image's width (Left) or when it's greater than half (Right)

How do I change CCScrollLayer content size? (cocos2d extension class)

I am currently using the CCScrollLayer (in the cocos2d extension classes) class to implement a menu system. It's working great but I would like to have other buttons on the screen and the scrollable area is the entire screen by default.
I tried messing with content size but no dice. After doing some reading, I found that the content size is set to the screen size per CCLayers behavior. A user suggested wrapping it in a CCNode and scaling, but this did not help.
Any suggestions or sample code? I'd have to think this should be possible.
CAScrollLayer is surprisingly simple, which may be confusing.
Just add the content layer to it:
[_scrollLayer addSublayer:_contentLayer];
To set the rect you want to be visible on the screen set bounds or frame of the scroll layer:
[_scrollLayer setBounds:visibleBounds];
Set the content's size respectively. The content's size can be bigger or smaller, it does not matter.
[_contentLayer setBounds:currentContextBounds];
If the content is bigger and you want to scroll to a certain point use scrollToPoint: or scrollToRect: methods of the scroll layer.
You need to implement your own scrolling indicators, bars, etc. if desired.
Although I am using table view and not the scroll view, both share the same parent [scroll view doh].
Have you turned on clipToBounds after setting content size?
I would recommend using this constructor:
/**
* Init this object with a given size to clip its content.
*
* #param size view size
* #return initialized scroll view object
*/
- (id)initWithViewSize:(CGSize)size;
Thanks everyone for the help, unfortunately it seems you cannot change the size of a CCLayer (or the contentSize has no effect on touch events) without basically rewriting a lot of code.
However, I found a workaround that seems to be working. In the ccTouch events began and moved, I check my desired bounds after converting my UITouch to cocos2D space. If they are not in the my bounds I return, and in the case of move I manually call TouchEnded. TouchEnded also checks the initial touched point, and if it is was out of bounds it ignores it.
Things seems to be working as desired. I will post more information if I find it. Thanks again everyone.

Graphic scrolling in iPhone SDK

I have a graphic that is 3 times the width of an iphone landscape view.
I am trying to auto scroll it so that it appears that it is moving sideways, without using the touchscreen scrolling method.
My aim is to maybe have a button you can press and it moves it left or right across the screen like an animation.
I can deal with everything else but am having trouble finding a solution.
Any example code would be appreciated or even any info on whether it is possible or not.
Thanks. Dave
You can wrap a UIView in an animation block. The animation sweeps the origin value in its frame property from one point to another, over a set period of time.