Avoider Game code

I was checking out the sample Avoider game from the hour of code webisode and notice that my computer starts to lag behind the movement and the animation also slows when I add a sound effect.

Anyone spot what is wrong?

http://www.sallyfitz.com/HourOfCode-iOSExamples.zip

There was another member with the same problem a while ago https://forum.xojo.com/20088-playing-sound-high-resources/0

Hi Michel, it doesn’t look like Dave posted a resolution to the problem.

Indeed. That was just a way to bring that history to the thread.

There are suggestions in there, though.

I am afraid you will have to scour Internet and possibly Stack Overflow to look for some way.

Since early 2015, some technical evolutions today may permit more than then.

I am sure there are ways, given the number of apps around that play music continuously without appreciable slow down.

Seem multicore programming is not infrequent in iOS. Maybe you can find some ideas
https://duckduckgo.com/?q=ios+multicore+programming&ia=qa

BTW at the end of the other thread I mentioned Xojo iOS could not launch another process. That is not entirely true.

Already, Jason King has implemented ShowURL which launches Safari with a page. I cannot quite remember where, but it seems one can launch an app from its identifier. If that was the case, then you could like we do in iOS place a helper within the bundle and launch it at the start of the program.

Maybe someone more in tune with the iOS framework will chime in.

I think I found what is needed to open another app by its identifier akin to OS X NSWorkspace launchAppWithBundleIdentifier used to launch helpers :
https://developer.apple.com/reference/uikit/uiapplication/1648685-open

This seems to be exactly what is needed to launch a helper.

This is way over my head since I suffer intense heartburns whenever I try to use Objective-C, but someone more versed into these arcanes could probably come up with a declare.

Try the following untested code to play a sound looping (I just typed it into the forum so it might not compile without a few modifications but they should be slight). The code assumes a sound called “MySound.mp3” is included next to your app copied with a copy files step and requires iOSKit.

//in a method play/open evnt/etc
using Xojo.IO
using Foundation
using AVFoundation
dim f as folderitem = SpecialFolder.getResource("MySound.mp3")
dim URL as new nsurl(f)
dim err as nserror
player = new avaudioplayer(f, err) //have player as avfoundation.avaudioplayer as a property on your view
player.numberofloops = -1 //for infinite times. otherwise set a positive number if its a limited number
player.play

//in another method to stop the sound/view close event/etc
//you MUST call stop if you have number of loops = -1
player.stop

[quote=309202:@Michel Bujardet]I think I found what is needed to open another app by its identifier akin to OS X NSWorkspace launchAppWithBundleIdentifier used to launch helpers :
https://developer.apple.com/reference/uikit/uiapplication/1648685-open

This seems to be exactly what is needed to launch a helper.

This is way over my head since I suffer intense heartburns whenever I try to use Objective-C, but someone more versed into these arcanes could probably come up with a declare.[/quote]
AFAIK you can’t use a helper process on iOS (unless it’s changed for ios10). You can launch another app if they have the proper URL scheme, but you can’t make a helper app which runs in the background only.

So anyway, what I found is as close as it gets to launch, right ? Even if not for a helper, it could be very nice to be able to launch another application.

If the app has the proper url scheme setup then yes, you can launch another app. But you can’t necessarily launch any arbitrary app since they won’t all have url schemes registered. Take google drive for example. All of the associated apps have a url scheme registered so the main app can open the slides app with a presentation.

[quote=309243:@Jason King]Try the following untested code to play a sound looping (I just typed it into the forum so it might not compile without a few modifications but they should be slight). The code assumes a sound called “MySound.mp3” is included next to your app copied with a copy files step and requires iOSKit.

[code]
//in a method play/open evnt/etc
using Xojo.IO
using Foundation
using AVFoundation
dim f as folderitem = SpecialFolder.getResource(“MySound.mp3”)
dim URL as new nsurl(f)
dim err as nserror
player = new avaudioplayer(f, err) //have player as avfoundation.avaudioplayer as a property on your view
player.numberofloops = -1 //for infinite times. otherwise set a positive number if its a limited number
player.play

//in another method to stop the sound/view close event/etc
//you MUST call stop if you have number of loops = -1
player.stop
[/code][/quote]

Hi Jason, I just dragged all the ioskit modules into the project and added a method which I called from the open event for the code above but it produces hundreds of errors. I can see the avfoundation.avaudioplayer class but it says it isn’t there.??

Maybe remove the using statements then and add the full module scoping to all of the declarations. Also how did you drag the modules? I would make sure you open the example project and then Copy the modules and paste them into your project. Dragging may not work correctly.

Closer now only 2 errors after adding the module properly.

It gives an error of “There is more than one item with this name and it is not clear which it refers to” for
player = new avaudioplayer(f, err)

Also

player.play gives an error you must use the value returned

Sorry, change f to URL. Also add “call” before player.play (or set a Boolean to it to know if it successfully started playing).

It would be nice to post the working code, eventually.

[code]//in a method play/open evnt/etc
using Xojo.IO
using Foundation
using AVFoundation
dim f as folderitem = SpecialFolder.getResource(“MySound.mp3”)
dim URL as new nsurl(f)
dim err as nserror
player = new avaudioplayer(url, err) //have player as avfoundation.avaudioplayer as a property on your view
player.numberofloops = -1 //for infinite times. otherwise set a positive number if its a limited number
call player.play

//in another method to stop the sound/view close event/etc
//you MUST call stop if you have number of loops = -1
player.stop[/code]

Thank you Jason :slight_smile:

I still have a small problem with the location of the sound not being found. I simply left it in project’s root directory??

dim f as folderitem = SpecialFolder.getResource(“MySound.mp3”)

Sorry just misinterpreting what Jason meant. Works much better without any slow downs :slight_smile: