Tipp 0167 Sound-Animation
Autor/Einsender:
Datum:
  Richard Schubert
28.11.2001
Entwicklungsumgebung:
DirectX-Version:
  VB 6
DirectX 7
Bei diesem Tipp wurden verschiedene DirectSound-Möglichkeiten mit einer kleiner Animation verknüpft.
 
Option Explicit

Dim m_dx As New DirectX7
Dim m_ds As DirectSound
Dim m_dsBuffer(2) As DirectSoundBuffer
Dim SoundFile(2)

Dim bufferDesc As DSBUFFERDESC
Dim waveFormat As WAVEFORMATEX

Dim m As Integer
Dim n As Integer
Dim XSpeed As Integer
Dim YSpeed As Integer

Private Sub Form_Load()
  Set m_ds = m_dx.DirectSoundCreate("")

  m_ds.SetCooperativeLevel Me.hWnd, DSSCL_PRIORITY

  LoadWave 0, App.Path + "\loop.wav"
  LoadWave 1, App.Path + "\drop.wav"
  LoadWave 2, App.Path + "\glass.wav"

  m_dsBuffer(0).SetVolume -1000
  m_dsBuffer(0).Play 1

  XSpeed = 1
  YSpeed = 1

  Me.Show

  Do
    DoEvents
    moveBall

    For m = 0 To 20000
    Next
  Loop
End Sub

Sub LoadWave(I As Integer, sfile As String)
  bufferDesc.lFlags = DSBCAPS_CTRLFREQUENCY Or _
    DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC

  waveFormat.nFormatTag = WAVE_FORMAT_PCM
  waveFormat.nChannels = 10
  waveFormat.lSamplesPerSec = 22050
  waveFormat.nBitsPerSample = 16
  waveFormat.nBlockAlign = _
    waveFormat.nBitsPerSample / 8 * waveFormat.nChannels
  waveFormat.lAvgBytesPerSec = _
    waveFormat.lSamplesPerSec * waveFormat.nBlockAlign
  Set m_dsBuffer(I) = _
    m_ds.CreateSoundBufferFromFile(sfile, bufferDesc, waveFormat)
End Sub

Sub moveBall()
  Ball.Left = Ball.Left + XSpeed
  Ball.Top = Ball.Top + YSpeed

  If Ball.Top < 0 Then
    YSpeed = -YSpeed
    If m_dsBuffer(1).GetStatus Then
      m_dsBuffer(1).Stop
      m_dsBuffer(1).SetCurrentPosition 0
    End If
    m_dsBuffer(1).Play 0
  End If

  If Ball.Top + Ball.Height > Me.ScaleHeight Then
    YSpeed = -YSpeed
    If m_dsBuffer(1).GetStatus Then
      m_dsBuffer(1).Stop
      m_dsBuffer(1).SetCurrentPosition 0
    End If
    m_dsBuffer(1).Play 0
  End If

  If Ball.Left < 0 Then
    XSpeed = -XSpeed
    If m_dsBuffer(1).GetStatus Then
      m_dsBuffer(1).Stop
      m_dsBuffer(1).SetCurrentPosition 0
    End If
    m_dsBuffer(1).Play 0
  End If

  If Ball.Left + Ball.Width > Me.ScaleWidth Then
    XSpeed = -XSpeed
    If m_dsBuffer(1).GetStatus Then
      m_dsBuffer(1).Stop
      m_dsBuffer(1).SetCurrentPosition 0
    End If
    m_dsBuffer(1).Play 0
  End If

  For n = 0 To 4
    If Ball.Left < Glass(n).X1 Then
      XSpeed = -XSpeed
      m_dsBuffer(2).Play 0
      Glass(n).X1 = -100
      Glass(n).Visible = False
    End If
  Next

  For n = 5 To 9
    If Ball.Left + Ball.Width > Glass(n).X1 Then
      XSpeed = -XSpeed
      m_dsBuffer(2).Play 0
      Glass(n).X1 = 10000
      Glass(n).Visible = False
    End If
  Next
End Sub
 
Weitere Links zum Thema
Sound-Dateien laden, abspielen, wiederholen
Hinweis
Um dieses Beispiel ausführen zu können, wird die DirectX 7 for Visual Basic Type Library benötigt (siehe dazu die Erläuterungen in der DirectX-Rubrik).

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (73 kB) Downloads bisher: [ 1595 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Samstag, 24. September 2011