iOS Question Playing audio (iSpritekit)

ilan

Expert
Licensed User
Longtime User
hi

i am trying iSpritekit for the last few days and i really like it.
Although i have some questions.

1, i am playing audio on my game with a SKaction and i wanted to know if this is the right way to play audio files (background music + sound effects)

2, when i add a SKspritenode to my SKscene i noticed that the "ToucheBegan" event of the SKspritenode is not triggered because the "ToucheBegan" of Skscene is triggered. so my question is, is this the normal behavior? if the parent has a "ToucheBegan" event and the child also will always the parent event triggered?? i would like to use an SKspritenode as a button and want to know when someone clicks on it.
what i do now to solve the problem is i check the x/y of the aTouch of the parent and if it is in a specific area i know that the button was clicked but it would be easier to get directly the touch of the button.

those are my questions for now (more will follow soon :D)

thank you, ilan
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

1: There are two options in SpriteKit for playing music/sounds: SKAction and SKAudioNode. The only advantage of the SKAudioNode is, that you have some action possibilities (with SKAction object), for example "ChangeVolumeTo". You can use the SKAudioNode the following way:
B4X:
    Dim Music As SKAudioNode
    Music.Initialize("")
    Music.AudioNodeWithURL(File.Combine(File.DirAssets,"music.mp3"))
    Music.AutoplayLooped = True
   
    GameScene.AddChild(Music)

The music automatically starts to play when you add it to a node.

2:
is this the normal behavior?
No. Here it works fine, both events are triggered. Can you upload an example project?

Jan
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
1: There are two options in SpriteKit for playing music/sounds: SKAction and SKAudioNode. The only advantage of the SKAudioNode is, that you have some action possibilities (with SKAction object), for example "ChangeVolumeTo". You can use the SKAudioNode the following way:

ok thanks this is very good to know. so i guess i will use SKAudioNode for backgroundmusic and SKAction for soundeffects.


No. Here it works fine, both events are triggered. Can you upload an example project?

ok i will try to do it when i get home. it happened to me in my Pong! game. the pause button is a SKSpriteNode and i add it to the GameScene (SKScene) with .addchild but clicking on it triggered the Parent (GameScene) Touchbegan event.

EDIT: i have the filling that i need to change the zPosition of the SKSpriteNode :rolleyes:


btw is it possible to apply an impulse only to the x axis? or change the velocity to 1 axis. the thing is that when the ball hits the Pad i would like it to go to the x axis according to the collisions point. the problem is i have to add the impulse or change the velocity as an vector of x and y but i dont want to change the velocity of the y axis. and applying the same velocity to the y axis does not work.

pad1.png


getting the point and calculating the x according to the angle is not a problem the only thing is, i dont use gravity in my game (also not angular damping or linear damping).
pads + gamescene frame physicbodies have restitution set to 1 and friction set to 0

now the ball is moving in a constant speed. but i want to control the x velocity of it without effecting the y velocity.

do you have any idea?

i tried it like this:

B4X:
    Dim v0, v1 As Vector
    v0.Initialize(0,0)
    v1.Initialize(myXvel,ball.PhysicsBody.Velocity.Dy)

    ball.PhysicsBody.Velocity = v0
    ball.PhysicsBody.ApplyImpulse = v1

the x value is ok but the y not because after the hit it changes so this is not working well. i want only to change the x value. is this possible?

thank you
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
maybe i should do it in the "DidEndContact" event and not in the "DidBeginContact".

like this the y value is already correct (and updated) and then i could change the x value.

will try it out.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
No. Here it works fine, both events are triggered. Can you upload an example project?

i have tried your Breakout example and added an touch event to the paddle. clicking on it will not log anything so it seems like SKSpritekit Touch event is not triggered.

this is what i have added to the project:

B4X:
Sub Paddle_ToucheBegan (aTouch As Touch)
    Log("clicked on paddle")
End Sub

i have also commented this (because i thought maybe this is blocking the touch event to be triggered) but did not help

B4X:
'Sub Breakout_ToucheBegan (aTouch As Touch)
'    Dim TouchLocation As Point = aTouch.LocationInNode(Breakout)
'   
'    Dim Body As SKPhysicsBody = Breakout.PhysicsWorld.BodyAtPoint(TouchLocation)
'    If Body.IsInitialized And Body.Node.Name = CategoryName_Paddle Then
'        IsFingerOnPaddle = True
'    End If
'End Sub

no logs are showing when i tap on the paddle
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
No. Here it works fine, both events are triggered. Can you upload an example project?

ok i found out what i have to do.

B4X:
pausebtn.UserInteractionEnabled = True

after setting the SKSpriteNode userinteractionenabled to true the touches are triggered ;)
 
Upvote 0
Top