📄 nokiagameeffects.java
字号:
//
// Copyright 2002 Nokia Corporation
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Mobile
// Phones Ltd. retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Third-party brands and names are the property of their respective
// owners. Java(TM) and J2ME(TM) are registered trademarks of
// Sun Microsystems Inc.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
//
import com.nokia.mid.sound.Sound;
import com.nokia.mid.ui.DeviceControl;
import com.nokia.mid.ui.DirectUtils;
import com.nokia.mid.ui.DirectGraphics;
import javax.microedition.lcdui.*;
import java.io.IOException;
// Nokia GameEffects provides a 'Nokia device specific' implementation
// of game effects such as vibration, flashing lights and
// playing game sounds.
class NokiaGameEffects
extends GameEffects
{
private volatile boolean isPaused = true;
private Sound sound = null;
private volatile int currentPriority = 0;
// The sound byte arrays are in 'Smart Messaging' ringing tone format.
// Bullet 'ringing tone' sound effect
private final static byte[] COMPLETE_SOUND_BYTES =
{
(byte)0x02, (byte)0x4A, (byte)0x3A, (byte)0x80,
(byte)0x40, (byte)0x00, (byte)0xD2, (byte)0x09,
(byte)0x2D, (byte)0x02, (byte)0xD0, (byte)0x2D,
(byte)0x03, (byte)0x48, (byte)0x20, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x00
};
// Base explosion 'ringing tone' sound effect
private final static byte[] CRASH_SOUND_BYTES =
{
(byte)0x02, (byte)0x4A, (byte)0x3A, (byte)0x80,
(byte)0x40, (byte)0x01, (byte)0x32, (byte)0x08, (byte)0x21, (byte)0x02, (byte)0x10,
(byte)0x39, (byte)0x03, (byte)0x70, (byte)0x35,
(byte)0x03, (byte)0x30, (byte)0x30, (byte)0xc0,
(byte)0x00, (byte)0x00, (byte)0x00
};
NokiaGameEffects()
{
}
// Game Settings methods
boolean hasSoundCapability()
{
return true;
}
boolean hasVibrationCapability()
{
return true;
}
// Game Effects methods
private synchronized void setIsPaused(boolean isPaused)
{
this.isPaused = isPaused;
}
void pause()
{
setIsPaused(true);
if (sound != null)
{
sound.stop();
}
DeviceControl.stopVibra();
}
void resume()
{
// Note: The 'Game Settings' may change during a pause.
// In general, one might need to handle that during the resume.
// But we don't need to do anything to handle that before resuming
// because any sound or vibration that were in progress just
// before pausing were cancelled when pause() was called,
// and any 'Game Settings' changes will take effect after the resume.
setIsPaused(false);
}
private void playSound(int priority, byte[] bytes)
{
if (!isPaused && hasSoundCapability() && Settings.getUseSound())
{
try
{
if (sound == null || sound.getState() != Sound.SOUND_PLAYING)
{
sound = new Sound(bytes, Sound.FORMAT_TONE);
sound.setGain(128);
sound.play(1); // loop = 1
currentPriority = priority;
}
else
{
// A sound exists in the SOUND_PLAYING state.
if (priority > currentPriority)
{
sound.stop();
sound.init(bytes, Sound.FORMAT_TONE);
sound.play(1); // loop = 1
currentPriority = priority;
}
// else
// The last sound is still playing and has a higher
// priority than this play request, so we let the
// last sound continue to play and ignore this play
// request. We don't queue this play request for playing
// after the last one finishes, as that might sound
// funny to the user. (The sounds should match visual
// events occurring in the game). In a fast moving
// game, new play requests are likely to occur
// continuously and frequently (e.g. 'fire a bullet'
// noise), so we can cautiously drop a few play
// requests when it is sensible to do so.
}
}
catch (Exception e)
{
currentPriority = 0; // allow any new request to play
}
}
}
void playComplete()
{
// Lowest priority game sound
playSound(1, COMPLETE_SOUND_BYTES);
}
void playCrash()
{
// Higher priority game sound
playSound(1, CRASH_SOUND_BYTES);
}
void vibrate()
{
if (Settings.getUseVibration())
{
try
{
// Vibrate at 100% for 300 milli-seconds.
DeviceControl.startVibra(100, 300);
}
catch (Exception e)
{
// An IllegalStateException is thrown if the device doesn't
// have vibration support, or is in it's battery charging
// station, etc. We can't do anything, so just ignore it.
}
}
}
// My Gfx additions
//
Image getGfx(String str)
{
Image grafix;
try
{
grafix = Image.createImage(str);
}
catch (IOException e)
{
grafix = null;
}
return grafix;
}
void lightsOn()
{
DeviceControl.setLights(0,50);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -