Hi,
I tried following an older post about playing random sounds but the sounds from the arrays are not playing.
I’ve bolded the edits I made. Please help.
local composer = require( “composer” )
local scene = composer.newScene()
local ragdogLib = require “ragdogLib”;
local networksLib = require “networksLib”;
–local adsLib = require “adsLib”;
– All code outside of the listener functions will only be executed ONCE unless “composer.removeScene()” is called.
– local forward references should go here
ads.show( “banner”, { appId= “ca-app-pub-7850831907211777/7996895446”, x=0, y=100000 } )
–let’s localize these values for faster reading
local totalWidth = _G.totalWidth;
local totalHeight = _G.totalHeight;
local leftSide = _G.leftSide;
local rightSide = _G.rightSide;
local topSide = _G.topSide;
local bottomSide = _G.bottomSide;
local centerX = display.contentCenterX;
local centerY = display.contentCenterY;
local buttonSFX = _G.buttonSFX;
– FOR REFERENCE:
– local poofSheetData = {width = 165, height = 180, numFrames = 5, sheetContentWidth = 825, sheetContentHeight = 180}
– local poofSheet = graphics.newImageSheet(“images/characters/poof.png”, poofSheetData)
– local poofSequenceData = {
– {name = “poof”, start = 1, count = 5, time = 250, loopCount = 1}
– }
local explosionSheetData = {width = 100, height = 100, numFrames = 8, sheetContentWidth = 800, sheetContentHeight = 100}
local explosionSheet = graphics.newImageSheet(“IMG/explosion.png”, explosionSheetData)
local explosionSequenceData = {
{name = “explosion”, start = 1, count = 8, time = 200, loopCount = 1}
}
local explosion
local circleScale = .8;
local arrowSpeed = 10;
local circleInitialSpeed = 1;
local speedIncreaseAtEveryPoint = .5;
local maxCircleSpeed = 20;
local circleSpeed;
local currentScore;
local minAngleToMakePoint = 330;
local maxAngleToMakePoint = 32;
local minRedAngleToMakePoint = 355;
local maxRedAngleToMakePoint = 4;
local pointsForHittingRed = 2;
local gameOverSFX, scorePointSFX, hittingRedSFX, shootSFX;
– “scene:create()”
function scene:create( event )
--adsLib.showAd(“main_menu”);
local sceneGroup = self.view;
circleSpeed = 0;
currentScore = 0;
gameOverSFX = audio.loadSound(“SFX/blast.wav”);
scorePointSFX = {}
scorePointSFX[01] = audio.loadSound(“SFX/scorePointSFXTrump/trump01.mp3”);
scorePointSFX[02] = audio.loadSound(“SFX/scorePointSFXTrump/trump02.mp3”);
scorePointSFX[03] = audio.loadSound(“SFX/scorePointSFXTrump/trump03.mp3”);
scorePointSFX[04] = audio.loadSound(“SFX/scorePointSFXTrump/trump04.mp3”);
scorePointSFX[05] = audio.loadSound(“SFX/scorePointSFXTrump/trump05.mp3”);
scorePointSFX[06] = audio.loadSound(“SFX/scorePointSFXTrump/trump06.mp3”);
scorePointSFX[07] = audio.loadSound(“SFX/scorePointSFXTrump/trump07.mp3”);
scorePointSFX[08] = audio.loadSound(“SFX/scorePointSFXTrump/trump08.mp3”);
scorePointSFX[09] = audio.loadSound(“SFX/scorePointSFXTrump/trump09.mp3”);
scorePointSFX[10] = audio.loadSound(“SFX/scorePointSFXTrump/trump10.mp3”);
scorePointSFX[11] = audio.loadSound(“SFX/scorePointSFXTrump/trump11.mp3”);
scorePointSFX[12] = audio.loadSound(“SFX/scorePointSFXTrump/trump12.mp3”);
scorePointSFX[13] = audio.loadSound(“SFX/scorePointSFXTrump/trump13.mp3”);
scorePointSFX[14] = audio.loadSound(“SFX/scorePointSFXTrump/trump14.mp3”);
scorePointSFX[15] = audio.loadSound(“SFX/scorePointSFXTrump/trump15.mp3”);
scorePointSFX[16] = audio.loadSound(“SFX/scorePointSFXTrump/trump16.mp3”);
scorePointSFX[17] = audio.loadSound(“SFX/scorePointSFXTrump/trump17.mp3”);
scorePointSFX[18] = audio.loadSound(“SFX/scorePointSFXTrump/trump18.mp3”);
scorePointSFX[19] = audio.loadSound(“SFX/scorePointSFXTrump/trump19.mp3”);
scorePointSFX[20] = audio.loadSound(“SFX/scorePointSFXTrump/trump20.mp3”);
scorePointSFX[21] = audio.loadSound(“SFX/scorePointSFXTrump/trump21.mp3”);
scorePointSFX[22] = audio.loadSound(“SFX/scorePointSFXTrump/trump22.mp3”);
scorePointSFX[23] = audio.loadSound(“SFX/scorePointSFXTrump/trump23.mp3”);
scorePointSFX[24] = audio.loadSound(“SFX/scorePointSFXTrump/trump24.mp3”);
scorePointSFX[25] = audio.loadSound(“SFX/scorePointSFXTrump/trump25.mp3”);
hittingRedSFX = {}
hittingRedSFX[26] = audio.loadSound(“SFX/hittingredTrump/trump26.mp3”);
hittingRedSFX[27] = audio.loadSound(“SFX/hittingredTrump/trump27.mp3”);
hittingRedSFX[28] = audio.loadSound(“SFX/hittingredTrump/trump28.mp3”);
hittingRedSFX[29] = audio.loadSound(“SFX/hittingredTrump/trump29.mp3”);
hittingRedSFX[30] = audio.loadSound(“SFX/hittingredTrump/trump30.mp3”);
hittingRedSFX[31] = audio.loadSound(“SFX/hittingredTrump/trump31.mp3”);
hittingRedSFX[32] = audio.loadSound(“SFX/hittingredTrump/trump32.mp3”);
hittingRedSFX[33] = audio.loadSound(“SFX/hittingredTrump/trump33.mp3”);
hittingRedSFX[34] = audio.loadSound(“SFX/hittingredTrump/trump34.mp3”);
hittingRedSFX[35] = audio.loadSound(“SFX/hittingredTrump/trump35.mp3”);
hittingRedSFX[36] = audio.loadSound(“SFX/hittingredTrump/trump36.mp3”);
hittingRedSFX[37] = audio.loadSound(“SFX/hittingredTrump/trump37.mp3”);
hittingRedSFX[38] = audio.loadSound(“SFX/hittingredTrump/trump38.mp3”);
hittingRedSFX[39] = audio.loadSound(“SFX/hittingredTrump/trump39.mp3”);
hittingRedSFX[40] = audio.loadSound(“SFX/hittingredTrump/trump40.mp3”);
hittingRedSFX[41] = audio.loadSound(“SFX/hittingredTrump/trump41.mp3”);
hittingRedSFX[42] = audio.loadSound(“SFX/hittingredTrump/trump42.mp3”);
hittingRedSFX[43] = audio.loadSound(“SFX/hittingredTrump/trump43.mp3”);
hittingRedSFX[44] = audio.loadSound(“SFX/hittingredTrump/trump44.mp3”);
hittingRedSFX[45] = audio.loadSound(“SFX/hittingredTrump/trump45.mp3”);
hittingRedSFX[46] = audio.loadSound(“SFX/hittingredTrump/trump46.mp3”);
hittingRedSFX[47] = audio.loadSound(“SFX/hittingredTrump/trump47.mp3”);
hittingRedSFX[48] = audio.loadSound(“SFX/hittingredTrump/trump48.mp3”);
hittingRedSFX[49] = audio.loadSound(“SFX/hittingredTrump/trump49.mp3”);
hittingRedSFX[50] = audio.loadSound(“SFX/hittingredTrump/trump50.mp3”);
shootSFX = audio.loadSound(“SFX/shootSFX.mp3”);
local group = self.view;
local bg = display.newImageRect(group, “IMG/bg.png”, totalWidth, totalHeight);
bg.x, bg.y = centerX, centerY;
local circle = display.newImageRect(group, “IMG/circle.png”, 164*circleScale, 164*circleScale);
local yuck = display.newImageRect(group, “IMG/yuck.png”, 164*circleScale, 164*circleScale);
circle.x, circle.y = centerX, centerY-140;
yuck.x, yuck.y = centerX, centerY-140;
function circle:enterFrame()
self.rotation = self.rotation+circleSpeed;
if self.rotation > 360 then
self.rotation = self.rotation-360;
end
yuck.rotation = self.rotation;
end
Runtime:addEventListener(“enterFrame”, circle);
circle:toFront();
yuck.alpha = 0;
local line = display.newImageRect(group, “IMG/line.png”, 441, 3);
line.x, line.y = centerX, centerY+180;
local line = display.newImageRect(group, “IMG/line.png”, 441, 3);
line.x, line.y = centerX, centerY+180;
local arrow = display.newImageRect(group, “IMG/triangle.png”, 40, 40);
arrow.x, arrow.y = centerX, line.y-line.contentHeight*.5-arrow.contentHeight*.5-5;
arrow.xStart, arrow.yStart = arrow.x, arrow.y;
arrow.state = 0;
arrow.rotation = 0;
function arrow:enterFrame()
if self.state == 1 then
self.rotation = self.rotation + 10
if self.rotation > 360 then
self.rotation = self.rotation-360;
end
if self.y-self.contentHeight*.5 <= circle.y+circle.height*.5 then
if circle.rotation >= minAngleToMakePoint or circle.rotation <= maxAngleToMakePoint then
if circleSpeed == 0 then
circleSpeed = circleInitialSpeed;
else
circleSpeed = circleSpeed+speedIncreaseAtEveryPoint;
if circleSpeed > maxCircleSpeed then
circleSpeed = maxCircleSpeed;
end
end
currentScore = currentScore+1;
if circle.rotation >= minRedAngleToMakePoint or circle.rotation <= maxRedAngleToMakePoint then
currentScore = currentScore+pointsForHittingRed;
local function playhittingRedSFX()
local thishittingRedSFX = math.random(#hittingRedSFX)
audio.play(hittingRedSFX[thishittingRedSFX])
end
audio.play(hittingRedSFX, {onComplete=playhittingRedSFX})
else
local function playscorePointSFX()
local thisplayscorePointSFX = math.random(#playscorePointSFX)
audio.play(playscorePointSFX[thisplayscorePointSFX])
end
audio.play(playscorePointSFX, {onComplete=playscorePointSFX})
end
yuck:toFront();
transition.to(yuck, {time = 100, alpha = 1, xScale = 1.1, yScale = 1.1});
transition.to(yuck, {delay = 800, time = 100, alpha = 0, xScale = 1, yScale = 1});
self.state = 0;
self.x, self.y = arrow.xStart, arrow.yStart;
else
self.isVisible = false;
self.state = 2;
self.time = 0;
audio.play(gameOverSFX, {channel = audio.findFreeChannel()});
for i = 1, 20 do
local particle = display.newRect(group, 0, 0, 5, 5);
particle.xSpeed = math.random(-30, 30)*.1;
particle.ySpeed = math.random(20, 40)*.1;
particle.x, particle.y = self.x, self.y-self.contentHeight*.5;
function particle:enterFrame()
self.x, self.y = self.x+self.xSpeed, self.y+self.ySpeed;
self.alpha = self.alpha-0.01;
if self.alpha <= 0 then
Runtime:removeEventListener(“enterFrame”, self);
self:removeSelf();
end
end
Runtime:addEventListener(“enterFrame”, particle);
circleSpeed = 0;
end
end
end
self.y = self.y-arrowSpeed;
elseif self.state == 2 then
self.time = self.time+1;
if self.time >= 90 then
Runtime:removeEventListener(“enterFrame”, self);
_G.currentScore = currentScore;
composer.gotoScene(“gameOverScene”, “fade”);
end
end
end
Runtime:addEventListener(“enterFrame”, arrow);
local tapToStart = display.newText(group, “Tap to Start”, 0, 0, native.systemFont, 25);
tapToStart.x, tapToStart.y = centerX, centerY;
function bg:touch(event)
if event.phase == “began” then
if arrow.state == 0 then
arrow.state = 1;
audio.play(shootSFX, {channel = audio.findFreeChannel()});
if tapToStart then
transition.to(tapToStart, {time = 200, alpha = 0, onComplete = tapToStart.removeSelf});
tapToStart = nil;
end
end
end
end
bg:addEventListener(“touch”, bg);
local scoreText = display.newText(group, currentScore, 0, 0, native.systemFont, 30);
scoreText.x, scoreText.y = rightSide-10-scoreText.contentWidth*.5, topSide+10+scoreText.contentHeight*.5;
function scoreText:enterFrame()
self.text = currentScore;
self.x, self.y = rightSide-10-self.contentWidth*.5, topSide+10+self.contentHeight*.5;
end
Runtime:addEventListener(“enterFrame”, scoreText);
end
– “scene:show()”
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == “did” ) then
– Called when the scene is now on screen.
– Insert code here to make the scene come alive.
– Example: start timers, begin animation, play audio, etc.
end
end
– “scene:hide()”
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
audio.dispose(gameOverSFX);
gameOverSFX = nil;
audio.dispose(scorePointSFX);
scorePointSFX = nil;
audio.dispose(hittingRedSFX);
hittingRedSFX = nil;
audio.dispose(shootSFX);
shootSFX = nil;
local removeAll;
removeAll = function(group)
if group.enterFrame then
Runtime:removeEventListener(“enterFrame”, group);
end
if group.touch then
group:removeEventListener(“touch”, group);
Runtime:removeEventListener(“touch”, group);
end
for i = group.numChildren, 1, -1 do
if group[i].numChildren then
removeAll(group[i]);
else
if group[i].enterFrame then
Runtime:removeEventListener(“enterFrame”, group[i]);
end
if group[i].touch then
group[i]:removeEventListener(“touch”, group[i]);
Runtime:removeEventListener(“touch”, group[i]);
end
end
end
end
removeAll(self.view);
– Called when the scene is on screen (but is about to go off screen).
– Insert code here to “pause” the scene.
– Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == “did” ) then
– Called immediately after scene goes off screen.
end
end
– “scene:destroy()”
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s view (“sceneGroup”).
– Insert code here to clean up the scene.
– Example: remove display objects, save state, etc.
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene