Corona SDK - Check if object has stopped moving - coronasdk

Hi i am making a game with Corona SDK. I have a moving ball (physical object) to monitor. I need an event when the ball stops. If i set DrawMode to hybrid, i can see the ball changes color when it stops moving. Is there any built-in event available to check if the ball is stopped moving or any other way to check? Any help is appreciated.

Ok, found what i wanted. There is boolean property for each object isAwake which returns the awake state of object. If ball is moving it returns true otherwise false. Now i am checking this property with 1500ms of timer.
http://docs.coronalabs.com/api/type/Body/isAwake.html
Please reply if anyone has better solution.

Related

Chess - iOS - SpriteKit - Human move is not displayed until computer thinking ends

I am developing an IOS Chess app using SpriteKit in Swift2. While playing the game against computer player, human move is not displayed until computer thinking ends. After computer thinking ends, both (human move and computer move) moves are displayed together.
Expected functionality: Once human plays his/her move, the move must be displayed on screen. Only after displaying the human move, computer thinking must start.
I initially had the call to computer thinking logic inside touchesBegan function. It didn't work as expected. Then I moved the call into touchesEnded function. The issue remained. Now I have placed the call inside update function. The issue still remains.
I couldn't find an answer for this issue. Is there any away to address this?
Thanks,
ArtBajji
If the problem is that the "computer" reacts to quickly after a human move, you could use a dispatch_after block implemented in your touchesEnded.
let delay = 0 //time in seconds
dispatch_after(delay, dispatch_get_main_queue()) {
//computer starts thinking
}

is there any way to detect an object is falling or get the speed/direction on axis-y in cocos2d?

I'm a newbie to Cocos2d and game development. Now I'm encountering a problem.
I have a figure and I want to add different animations to it such as jump and fall. For the jump it is fine because I will have a button to trigger jump and I can add animation to it. However, for falling down is another story:
At the beginning, I'm trying to use - (void) update() and it seems this method is for constant moving. Another attempt is try to add a callback for CCAction. However, this can only detect falling after jump.
I'm wondering is there an API to detect an object is falling, or get the speed/direction on axis-Y. Or I have to do it manually and listen frame by frame?
I really appreciate your help =)

How to successfully pause game in Corona SDK?

Im trying to learn Corona through someone else's sample code. So this is a game like fruit ninja, and im trying to put a pause/resume function on it. Now since the code uses physics, i thought that i should use physics.pause and physics.start and i also paused the timer for the objects. It does freezes the screen, but when you swipe at one of the objects on the screen(fruits), it still breaks into two. How do i stop that? So i guess the pause works a little, because it stops the fruits from coming up. Thanks so much for the people who will answer my question. Ive read a few forums on here and you guys seem to really know what youre doing. :)
From your line here:
Runtime:addEventListener("touch", drawSlashLine)
Is it possible to remove this event listener in your pause, and re-add it into your resume?
Actually that will just stop you from drawing. You need to loop through all objects and remove their touch event listener.
Alternatively set a global variable to true when paused, and in chopFruit function check it, and do nothing if its set to true.
You should add code similar to the following:
if gameIsActive then
gameIsActive = false
physics.pause()
Runtime:removeEventListener("enterFrame",moveEnemy)
leftarrow:removeEventListener( "touch", moveLeft )
end
Then on resume, you should add the event listeners back.

How to make toggle for iOS game to control movement?

I'm making an iOS game, and I want to be able to control the movement of my character with a toggle.
My idea is that when the user touches the screen with their thumb / finger, I will record where the touch began, and then whichever direction the users thumb is compared to where the touch began is the direction the character should move.
How could I setup a touch gesture recogniser to do this?
Cheers
Hmm, it sounds to me like you are thinking of this at too high of a level. You don't need a "gesture recognition system" to do this. You know how to tell whether and where the touch is down? You know how to tell what time it is? Each time through your main loop, check if the touch is down / still down. When it first goes down, record the location and time. If later you see that it hasn't gone up and it's positioned a certain number of pixels to the right, you know it slid over there. And by comparing the times you know how fast it slid over there.
And of course you might want to check out Cocos2d for a library that does some of this for you. But it's worth doing yourself first, just to learn it.

Trouble toggling the userInteractionEnabled property in iOS

I am developing a tic tac toe game for iOS and I am using a combination of UIButtons and UIImageViews to allow for user interaction and to display the moves made. My problem is that the buttons continue to accept user input before the cpu makes it's move, which breaks my game logic. I have made several attempts to toggle the userInteractionEnabled property, but I have only been able to turn it off. The engine that gets everything started in the game is my buttonPressed method. I also toggle the userInteractionEnabled property within this method and therein lies my problem: How do I re-enable the property after disabling user interaction? Is there a method that is called in between events that I can overwrite?
I have searched the web and I have searched through the developer documentation provided by Apple and I found information on the touchesBegan and touchesEnded methods. However, from what I understand, those methods need to be explicitly called which brings me back to my original problem of not being able to call those functions without the user's interaction.
If anyone can help, I would greatly appreciate it! I have been racking my brain over this for the past couple of weeks and I am just not seeing a solution.
I'd think that for a game like tic-tac-toe, calculating the countermove should be so fast that it can be done immediately in response to the first button press. If you're doing something complicated to calculate the next move, like kicking off a thread, you might want to reconsider that.
Let's say, though, that your game is something like chess or go, where coming up with a countermove might take a bit longer. Your view controller should have a method to make a move for the current player, let's call it -makeMove:. Your -buttonPressed action should call that method to make a move for the user. In response, -makeMove: should update the state of the game, switch the current player to the next player. If the new current player is the computer, it should then disable the controls and start the process of calculating the next move. Let's imagine that's done by invoking some NSOperation, so that coming up with the next move is an asynchronous task. Once the operation has come up with a move, it should again invoke -makeMove: (by calling -performSelectorOnMainThread:), which will again update the game state and the current player. This time, though, it should see that the new current player is not the computer, and so it should re-enable the controls.

Resources